Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP if not statements

This may be the way my server is set up, but I'm banging my head against the wall. I'm trying to say that if $action has no value or has a value that is not "add" or "delete" then have an error, else keep running the script. However, I get an error no matter what $action is.

 $action = $_GET['a'];
 if((!isset($action)) || ($action != "add" || $action != "delete")){
     //header("location:index.php");
     echo "error <br>";
 }

$action is being set properly and if run something like if($action =="add") it works. This is on my local host, so it could be a settings issue.

like image 487
Brooke. Avatar asked Jul 31 '10 05:07

Brooke.


People also ask

How do I negate an if statement in PHP?

A simple negation is to add parenthesis and negate the complete expression. Negation of a logical expression means, to negate all elements, and exchange && and || . For priority reasons, you have to add parenthesis (like here) or you can remove them.

How do you write not equal to condition in PHP?

Introduction to PHP not equal. One of the comparison operators in PHP is not equal, which is represented by the symbol != or <> and whenever we want to compare the data types of the two given values, we make use of not equal operator in PHP.

What are the 4 PHP conditional statements?

PHP Conditional Statements if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.

Can we use if without else in PHP?

An if statement by its self is absolutely fine. If you have a series of conditions where only one will be true it's better to put the most likely first, then use else if on the subsequent ones so that PHP doesn't have to keep evaluating each condition.


1 Answers

Your logic is slightly off. The second || should be &&:

if ((!isset($action)) || ($action != "add" && $action != "delete"))

You can see why your original line fails by trying out a sample value. Let's say $action is "delete". Here's how the condition reduces down step by step:

// $action == "delete"
if ((!isset($action)) || ($action != "add" || $action != "delete"))
if ((!true) || ($action != "add" || $action != "delete"))
if (false || ($action != "add" || $action != "delete"))
if ($action != "add" || $action != "delete")
if (true || $action != "delete")
if (true || false)
if (true)

Oops! The condition just succeeded and printed "error", but it was supposed to fail. In fact, if you think about it, no matter what the value of $action is, one of the two != tests will return true. Switch the || to && and then the second to last line becomes if (true && false), which properly reduces to if (false).

There is a way to use || and have the test work, by the way. You have to negate everything else using De Morgan's law, i.e.:

if ((!isset($action)) || !($action == "add" || $action == "delete"))

You can read that in English as "if action is not (either add or remove), then".

like image 68
John Kugelman Avatar answered Sep 30 '22 17:09

John Kugelman