Solved

PHP Syntax

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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