Operators In PHP

Logical Operators

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.