The Php 7 logical operator help us to filter data before displaying it. Our choices are AND, OR, XOR, NOT !, && and ||. In the last lesson we talked about Comparison Operators and how they work. We need to understand the logic of a situation so we know what to display and how. We will examine some scenarios to get a better understanding how this works.
Download files for this lesson here.
Php 7 Logical Operator “AND”
The Php 7 logical operator “AND” will examine whether 2 comparison operators meet a certain criteria. If they do the conditional results will display. Or else we can display something else. Consider this example. A crew of workers get off of work on Friday at 3pm. In our logical operator we will have to see what day it is and what time it is then display results accordingly.
/*IN THIS EXAMPLE FRIDAY AT 3PM IS QUITTING TIME FOR THE CREW HOWEVER IT IS THURSDAY SO WE WILL HAVE AN ALTERNATIVE LOGICAL ANSWER */ $today='Friday'; $quittingtime='3pm'; if($today=='Thursday' and $quittingtime=='3pm'){ echo 'It is QUITTING TIME'; }else{ echo 'Not Quitting Time Yet!'; }
Php 7 Logical Operator “OR”
The logical operator “OR” will check whether 1 of 2 conditions are met. If they are it will return true and we can display the results. If neither of the 2 conditions are true we will display a different result.
<?php /*IN THIS EXAMPLE WE USE THE "OR" LOGICAL OPERATOR. IF EITHER OF THE 2 CONDITIONS ARE MET THEN IT WILL DISPLAY A RESULT. IF NEITHER OF THE 2 CONDITIONS ARE MET WE WILL HAVE IT WILL DISPLAY A DIFFERENT RESULT */ $today='Friday'; $quittingtime='3pm'; if($today=='Thursday' or $quittingtime=='3pm'){ echo 'It is QUITTING TIME'; }else{ echo 'Not Quitting Time Yet!'; } ?>
Php 7 Logical Operator “XOR”
The logical operator “XOR” will return true if either condition is met. Our crew gets a break at 12pm and a break at 2pm. If either of those conditions are met the script will return true and we will display the result. If it is not true then we will display a different result.
<?php /*IN THIS EXAMPLE WE USE THE "XOR" LOGICAL OPERATOR. THE CREW GETS A BREAK AT NOON AND THEN AGAIN AT 2PM. IF EITHER OF THOSE CONDITIONS ARE MET OUR LOGIC WILL DISPLAY "BREAK TIME" OTHERWISE IT WILL DISPLAY "NOT BREAK TIME YET" */ $firstbreak='10am'; $secondbreak='1pm'; if($firstbreak=='12' xor $secondbreak=='2pm'){ echo 'Break Time'; }else{ echo 'Not Break Time Yet!'; } ?>
Php 7 Logical Operator “NOT !”
The Php 7 Logical Operator “Not” is an exclamation point (!). You should recall using this in the last lesson as a Comparison Operator. It is a logical operator as well. In our example if it is not 12 noon we will display a result that displays “Get Back To Work” otherwise we will display a result that displays “Break Time”.
<?php /*IN THIS EXAMPLE WE USE THE "NOT(!)" LOGICAL OPERATOR. */ $var='10am'; if($var !='12pm'){ echo 'Get To Work'; }else{ echo 'Break Time!'; } ?>
Php 7 Logical Operator “&&”
The Php 7 operator that has 2 ampersands “&&” works like the “AND” logical operator. I prefer to use this logical operator instead of “AND” most of the time. It is considered to have a greater precedence according to this article here. taking our code example from earlier we can write our code like this.
<?php /* && IS THE SAME AS "AND" BUT GREATER PRECEDENCE */ $today='Friday'; $quittingtime='3pm'; if($today=='Thursday' && $quittingtime=='3pm'){ echo 'It is QUITTING TIME'; }else{ echo 'Not Quitting Time Yet!'; } ?>
Php 7 Logical Operator “||”
The Php 7 logical operator with 2 pipes “||” is just like the “OR” logical operator. It is also considered to have greater precedence than using the “OR” logical operator.
/*IN THIS EXAMPLE WE USE THE "||" IT IS THE SAME AS THE "OR" LOGICAL OPERATOR WITH GREATER PRECEDENCE. IF EITHER OF THE 2 CONDITIONS ARE MET THEN IT WILL DISPLAY A RESULT. IF NEITHER OF THE 2 CONDITIONS ARE MET WE WILL HAVE IT WILL DISPLAY A DIFFERENT RESULT */ $today='Friday'; $quittingtime='3pm'; if($today=='Thursday' || $quittingtime=='3pm'){ echo 'It is QUITTING TIME'; }else{ echo 'Not Quitting Time Yet!'; }
A1WEBSITEPRO Social Media Pages
Here are my social media pages, lets hook up!