CopyPastehas never been so tasty!

Boolean Operators in PHP

by anonymous

  • 0
  • 0
  • 0
69 views

Orignal Source:http://codingvilla.com/boolean-operators-php-single-article-590.aspx

 

Boolean Operators in PHP:

 

Bool mean true or false so the operators dealing with these are and (&&), or (||), xor (xor), Not(!) and called Boolean operators.

 

Explanation:

 

And operators return true. If both operands ($var) are true. OR operators return true if either of the operand is true. Xor operator returns true when one of them is true not both having true value. Not operators take single operand and invert its value. Mean If the $var has true value so after invert its value will be false.  

 

And Operator:

 

  

  <?php

  $var1 = true;

   $var2 = false;

  //and operator return true when both operands have true value

  $and1 = (  $var1 and  $var1 )? "true":"false";

  $and2 = (  $var1 and $var2 )? "true":"false";

  $and3 = ( $var2 and $var2 )? "true":"false";

   print $result1 = "AND - 1:$and1 2:$and2 3:$and3<br>";

?>

 

 

Output:

 

 

AND - 1: true 2:false 3:false

 

 

OR Operator:

 

 

 

<?php

  $var1 = true;

   $var2 = false;

 

   //OR operator return true when wither operand have true value

  $OR4 = ( $var1 or $var1 )? "true":"false";

  $OR5 = ( $var1 or $var1 )? "true":"false";

  $OR6 = ( $var2 or $var2)? "true":"false";

  print $result2= "OR &nbsp;&nbsp;- 1:$OR4 2:$OR5 3:$OR6<br>";

?>

 

 

 

Output:

 

 

OR   - 1: true 2:true 3:false

 

 

XOR Operator:

 

 

<?php

  $var1 = true;

   $var2 = false;

 

  //xor operator return true when both operands have not same value

  $XOR7 = ( $var1 xor $var1 )? "true":"false";

  $XOR8 = ( $var1 xor $var2 )? "true":"false";

  $XOR9 = ( $var2 xor $var2 )? "true":"false";

 

 print $result3= "XOR - 1:$XOR7 2:$XOR8 3:$XOR9<br>";

 ?>

 

Output:

 

XOR - 1:false 2:true 3:false

 

Not Operator:

 

<?php

  $var1 = true;

   $var2 = false;

  //take one argument and inverts its sign

  $NOT10 = ( !$var1 )? "true":"false";

  $NOT11 = ( !$var2 )? "true":"false";

  print $result4= "NOT - 1:$NOT10 2:$NOT11";

?>

 

Output:

 

NOT - 1:false 2:true

Add A Comment: