Solved

PHP Syntax

Posted on 2014-09-11
5
146 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
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:yodercm
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:yodercm
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Alter an update query which rounds 7 30
MySQL Grouping 2 21
WordPress website error - Unable to access website. 5 31
Update data using formula 22 19
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now