Solved

PHP Syntax

Posted on 2014-09-11
5
143 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 82

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
'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 …
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 …

758 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

23 Experts available now in Live!

Get 1:1 Help Now