?
Solved

PHP Syntax

Posted on 2014-09-11
5
Medium Priority
?
159 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 84

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 2000 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 111

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

718 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