Mega Code Archive

 
Categories / Php / Operator
 

Logical Operators

Operator     Name     Returns True if...                               Example              Result   ||           Or       Left or right is true                            true || false        true   or           Or       Left or right is true                            true || false        true   xor          Xor      Left or right is true but not both               true xor true        false   &&           And      Left and right are true                          true && false        false   and          And      Left and right are true                          true && false        false   !            Not      The single operand is not true                   ! true               false   <?php    $a = true; $b = false;   #test both operands for true   $test1 = ( $a and $a )? "true":"false";    $test2 = ( $a and $b )? "true":"false";    $test3 = ( $b and $b )? "true":"false";    #test either operand for true   $test4 = ( $a or $a )? "true":"false";    $test5 = ( $a or $b )? "true":"false";    $test6 = ( $b or $b )? "true":"false";   #test for single operand is true   $test7 = ( $a xor $a )? "true":"false";    $test8 = ( $a xor $b )? "true":"false";    $test9 = ( $b xor $b )? "true":"false";   #invert values    $test10 = ( !$a )? "true":"false";    $test11 = ( !$b )? "true":"false";    $result = "AND - 1:$test1 2:$test2 3:$test3<br>";   $result .= "OR &nbsp;&nbsp;- 1:$test4 2:$test5 3:$test6<br>";   $result .= "XOR - 1:$test7 2:$test8 3:$test9<br>";   $result .= "NOT - 1:$test10 2:$test11";     echo( $result );  ?> <? $age  = 20; if (($age >= 13) && ($age < 65)) {    print "too old for a kid's discount and too young for the senior's discount."; } $meal = 'breakfast'; if (($meal == 'breakfast') || ($dessert == 'souffle')) {    print "Time to eat some eggs."; } ?>