Logical Operators : Logical Operators « Operator « PHP

Home
PHP
1.Chart
2.Class
3.Components
4.Cookie Session
5.Data Structure
6.Data Type
7.Date
8.Design Patterns
9.Development
10.DNS
11.Email
12.File Directory
13.Form
14.Functions
15.Graphics Image
16.HTML
17.Language Basics
18.Login Authentication
19.Math
20.MySQL Database
21.Network
22.Operator
23.PDF
24.Reflection
25.Statement
26.String
27.Utility Function
28.Web Services SOAP WSDL
29.XML
PHP » Operator » Logical Operators 
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.";
}
?>
  
  
Related examples in the same category
1.Logical Operators in Order of Precedence
2.Use the boolean compare operators
3.Greater than
4.Less than
5.Greate and equal
6.Less than and equal
7.Not equal: !=
8.Logic Operators in PHP
9.Or operator
10.Leap Year Determination
11.Negation operator
12.The not-equals operator
13.Relational Operators summary table
14.Relational Operators in action
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.