In this tutorial we learn about the logical operator and the use of logical operator in PHP. Logical operator are used to control program flow. Logical operator are used in transfer of control statements i.g in IF else statement or while loop statement etc and help to test the more then on condition at a time.
Below we briefly discuses each of the Logical operator used in PHP.
AND ( && ) Operator
When we use && symbols which is called AND operator. You can use Both AND or && symbols in PHP. AND operator return True when Both Condition return True. For example
if we check user name and password of the user in single statement then we use AND operator:
$username ='user'; $password ='password'; if ($username =='user' && $password =='password') { echo "Welcome to learning Hints.com" ; } else { print("Invalid username or password"); }
OR ( | | ) Operator
When we use || symbols which is called OR operator. You can use Both OR or || symbols in PHP. OR operator return True if any one expression return True form two or more given expression.
For example if any customer is Dealer OR Purchase grater then 100 books form books shop then 20% discount is given:
$customer_type =Normal; $book_quantity =102; if ($book_quantity >=100 | | $customer_type =='Dealer') { echo "25% discount" ; } else { echo "No Discount"; }
XOR Operator
If you have two condition X and Y. Then XOR return TRUE when either one condition is true from X and Y and return FALSE. if Both X and Y condition are TRUE or Both are FALSE.
Variables in PHP
PHP Concatenation
Calculation in PHP
If Statements in PHP
if ... else Statements in PHP
Nested IF Structure in PHP
PHP Comparison Operators
Switch Statement in PHP
Logical Operators In PHP
PHP Booleans
FOR Loop in PHP
While Loop In PHP
foreach Loop In PHP
Break Statement
Array In PHP
Associative Array
The Count function
Changing Case Function
Trim Function In PHP
Str_shuffle( ) function
strpos function in PHP
Explode() Function in PHP
Function
Passing Function Arguments
Server Variables
Header Function
Include Function
The require Function