Duration 8:3

Logical Operators - PHP (2022) - P19

88 watched
0
3
Published 5 Mar 2020

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. var_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. $a = "codeigniter"; $b = "laravel"; var_dump( $a == "codeigniter" && $b != "zend" ); // true var_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. 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. 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. var_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 choose 1 var_dump(true XOR true); // false Read the full article on Medium https://medium.com/dev-genius/php-7-x-p19-logical-operators-27441c69e060 Code for this tutorial https://github.com/dinocajic/php-7-youtube-tutorials/blob/master/19% 20Logical%20Operators.php Full Code https://github.com/dinocajic/php-7-youtube-tutorials PHP 2022 Playlist /playlist/PLbhJy0VhR6SAmoneYvF6Y_axQJKg7i50M -- Dino Cajic YouTuber, Author, and Head of IT Homepage: https://dinocajic.com Portfolio: http://dinocajic.xyz GitHub: https://github.com/dinocajic Medium: https://medium.com/@ dinocajic Instagram: https://www.instagram.com/think.dino/ LinkedIn: https://www.linkedin.com/in/dinocajic/ Twitter: https://twitter.com/dino_cajic My Book An Illustrative Introduction to Algorithms https://www.amazon.com/dp/1686863268

Category

Show more

Comments - 0