Solved

PHP Syntax

Posted on 2014-09-11
5
156 Views
Last Modified: 2014-09-11
How do I properly write the following:
<?php
if ((isset ($_POST['bike_state']) && $_POST['bike_state'] != '17') || (isset ($_POST['bike_state']) && $_POST['bike_state'] != '22') || (isset ($_POST['bike_state']) && $_POST['bike_state'] != '31'))  { }
?>

Open in new window

0
Comment
Question by:rgranlund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40317617
What's wrong with what you have?  Is there an error message?
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40317628
You should check to see if it's set before checking its value.
if (isset ($_POST['bike_state'])) {
	if ($_POST['bike_state'] != '17' && $_POST['bike_state'] != '22' && $_POST['bike_state'] != '31') {
	}
}

Open in new window

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40317706
The logic you are using will always succeed.  You have

if (
(isset ($_POST['bike_state']) && $_POST['bike_state'] != '17') ||
(isset ($_POST['bike_state']) && $_POST['bike_state'] != '22') ||
(isset ($_POST['bike_state']) && $_POST['bike_state'] != '31')
)  { }

If the value is 17 then it's not 22 and so the OR will succeed.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 40317714
If what you actually intend is to succeed if the value is set and is any value other than 17, 22, or 31, then you need to use:

if (
(isset ($_POST['bike_state']) &&
$_POST['bike_state'] != '17') &&
$_POST['bike_state'] != '22') &&
$_POST['bike_state'] != '31')
)  { }

This will succeed on 13, but fail on 17, 22, and 31.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40317885
You might want to learn about switch/case control structures.  For things like this it's a much more reasonable way to write the conditional statements.
http://www.php.net/manual/en/control-structures.switch.php
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question