PHP Logical Operators

Logic dictates.

PHP Logical operators are pretty simple if you’ve taken Boolean Algebra or are familiar with logic-gates. You might have even been exposed to logic in Philosophy. If you’re not familiar with logic, this may be a bit of a shock for you. Only joking. It’s just another operator in our disposal that evaluates to either true or false.

We looked at how to test whether an expression is true or not, but how can we test whether multiple expressions are true? With logical operators, that’s how.

There are a few operators in PHP that test the truth value of two expressions.

  • AND: returns true only if both the left and the right expressions evaluate to true.
  • &&: same as AND but with different precedence.
  • ||: returns true if either the left or the right, or both, expressions evaluate to true.
  • OR: same as || but with different precedence.
  • XOR: returns true only if either the left or the right expressions are true, but not both.
  • !: Negation. Returns true if false, and false if true.

 

<?phpvar_dump( true && true ); // evaluates to true$a = "lowered";
$b = "boosted";var_dump($a == "lowered" AND $b == "boosted"); // true?>

 

In the example above, with the expression true AND true, we have true on the left and true on the right sides. True AND True evaluates to true.

In the second example, we evaluate the expression on the left and the expression on the right. The expression on the left, $a == “lowered”, evaluates to true since $a does contain the string “lowered.” The expression on the right also evaluates to true since $b does contain the string “boosted.” Since both sides return true, we get true AND true. In order for the AND operator to return true, both the left and the right expressions have to evaluate to true. Since they do, it returns true.

We can use the negation operator to obtain the opposite value of the expression.

 

<?php$a = "codeigniter";
$b = "laravel";var_dump( $a == "codeigniter" && $b != "zend" ); // truevar_dump( !true ); // false
var_dump( !false ); // true?>

 

In the example above, the left side evaluates to true since $a does equal codeigniter. The expression on the right also evaluates to true since $b does not equal zend; that is a true statement. So true && true results in true.

Now that we understand the concept, the remainder of the logical operators should be a breeze.

 

<?php
var_dump( true and false); // false
?>

 

Any time that there’s a false in the AND statement, it will always evaluate to false. The example below also evaluates to false since it’s synonymous to AND, with the exception of precedence.

 

<?php
var_dump( true && false ); // false
?>

 

The || operator is called an “inclusive or” operator, meaning that you can have either side or both sides be true, and it will be true. The following results will yield true for the || operator:

  • true || false
  • false || true
  • true || true

It’s one thing to memorize the statements that evaluate to true, but it’s another thing to visualize it. If you take a class, the school might say, “in order to take this Advanced Math Class, you must have completed either Calculus 3 or Math Modeling.” If you have either of those classes completed, you’re good. If you took both of those classes, you’re also good to take the Advanced Math Class. The only time that you’re not going to be able to take the Advanced Math class is if you have neither of the required prerequisite classes completed.

 

<?phpvar_dump( true || false ); // true
var_dump( true || true); // true
var_dump( false || true); // true
var_dump( false || false); // false?>

 

You can use the OR operator instead of ||, but remember that OR and || have different precedence.

There is an exclusive-or operator, XOR, that is different than the inclusive-or operator that we just looked at. The XOR operator only evaluates to true when one side is true and the other is false.

  • true XOR false
  • false XOR true

Let’s try to visualize this as well. You might have been asked by your waiter whether you want the soup or salad with your dish. You choose one or the other, but not both. If this was an inclusive-or statement, you would be able to get the soup and salad, but we’re talking about exclusive-or’s, so you’ll have to choose one.

 

<?phpvar_dump(true XOR true); // false
var_dump(true XOR false); // true
var_dump(false XOR true); // true
var_dump(false XOR false); // false?>

 

You can compound the logical operators. Remember, based on precedence, one statement will be evaluated at a time. You’ll always have a left and a right expression.

 

<?phpvar_dump( true && (true || false) ); // true?>

 

In the example above, we evaluate the expression inside the parentheses first. True or false evaluates to true. We can then evaluate the next expression, true and true, which produces true.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Increment and Decrement Operators

But I don’t wanna Decrement.

PHP – P18: Increment and Decrement Operators

These are operators that either add 1 or subtract 1 from the current value. The operators can be prefixed or post-fixed and will yield slightly different results each time.

PHP Logical Operators

Logic dictates.

PHP – P19: Logical Operators

You might have even been exposed to logic in Philosophy. If you’re not familiar with logic, this may be a bit of a shock for you. Only joking. It’s just another operator in our disposal that evaluates to either true or false.

PHP String Operator

A STRING CAN OPERATE?

PHP — P20: STRING OPERATOR

The PHP string operator, also known as the concatenation operator, allows us to join two strings together. Strings need to be joined for various reasons, such as injecting a variable, or even splitting a long string so that it’s more readable in your IDE.

Leave a Reply