PHP/Operator/Logical Operators

Материал из Web эксперт
Версия от 10:06, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Greate and equal

   <source lang="html4strict">

<? $a = 21; $b = 15;

echo "

Original value of \$a is $a and \$b is $b

";

if ($a >= $b) {

echo "

TEST\$a is greater than or equal to \$b

";

} else {

echo "

TEST\$a is not greater than or equal to \$b

";

} ?>

      </source>
   
  


Greater than

   <source lang="html4strict">

<? $a = 21; $b = 15;

echo "

Original value of \$a is $a and \$b is $b

";

if ($a > $b) {

echo "

TEST \$a is greater than \$b

";

} else {

echo "

TEST \$a is not greater than \$b

";

} ?>

      </source>
   
  


Leap Year Determination

   <source lang="html4strict">

<?php function is_leap_year($year) {

   return ((($y % 4) == 0) && ((($y % 100) != 0) || (($y % 400) == 0)));

} foreach (range(1999, 2009) as $year) {

echo "

{$year} = ", is_leap_year($year) ? "Leap Year" : "not", "

";

} ?>

 </source>
   
  


Less than

   <source lang="html4strict">

<? $a = 21; $b = 15;

echo "

Original value of \$a is $a and \$b is $b

";

if ($a < $b) {

echo "

TEST\$a is less than \$b

";

} else {

echo "

TEST\$a is not less than \$b

";

} ?>

      </source>
   
  


Less than and equal

   <source lang="html4strict">

<? $a = 21; $b = 15;

echo "

Original value of \$a is $a and \$b is $b

";

if ($a <= $b) {

echo "

TEST\$a is less than or equal to \$b

";

} else {

echo "

TEST\$a is not less than or equal to \$b

";

} ?>

      </source>
   
  


Logical Operators

   <source lang="html4strict">

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
"; $result .= "OR   - 1:$test4 2:$test5 3:$test6
"; $result .= "XOR - 1:$test7 2:$test8 3:$test9
"; $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.";

} ?>

 </source>
   
  


Logical Operators in Order of Precedence

   <source lang="html4strict">


      </source>
   
  


Logic Operators in PHP

   <source lang="html4strict">

Operator Action

$foo and $bar TRue if $foo and $bar are TRue

$foo or $bar true if $foo or $bar is true

$foo xor $bar true if and only if $foo or $bar is true (only one or the other)

!$foo true if $foo is not true

$foo && $bar TRue if $foo and $bar are true

$foo || $bar true if $foo or $bar is true

 </source>
   
  


Negation operator

   <source lang="html4strict">

$first_name = "first"; $last_name = "last"; <? if (! strcasecmp($first_name,$last_name)) {

   print "$first_name and $last_name are equal.";

} ?>

 </source>
   
  


Not equal: !=

   <source lang="html4strict">

<? $a = 21; $b = 15;

echo "

Original value of \$a is $a and \$b is $b

";

if ($a != $b) {

echo "

TEST\$a is not equal to \$b

";

} else {

echo "

TEST\$a is equal to \$b

";

} ?>

      </source>
   
  


Or operator

   <source lang="html4strict">

<?php $user1 = "Paul"; $user2 = "Donna"; if ($username == $user1 || $username == $user2) {

   print "$username welcome."; 

} ?>

 </source>
   
  


Relational Operators in action

   <source lang="html4strict">

<html> <head>

  <title>Relational Operators</title>

</head> <body>

    <?php
       print("(5 < 5) is "  . ((5 < 5) ? "True" : "False") . "
"); print("(5 <= 5) is " . ((5 <= 5) ? "True" : "False") . "
"); print("(5 > 5) is " . ((5 > 5) ? "True" : "False") . "
"); print("(5 >= 5) is " . ((5 >= 5) ? "True" : "False") . "
");  ?>

</body> </html>

      </source>
   
  


Relational Operators summary table

   <source lang="html4strict">


      </source>
   
  


The not-equals operator

   <source lang="html4strict">

<? $new_messages = 10

if ($new_messages != 10) {

   print "You don"t have ten new messages.";

} if ($dinner != "B") {

   print "not b";

} ?>

 </source>
   
  


Use the boolean compare operators

   <source lang="html4strict">

<? $three = 3; $four = 4; $my_pi = 3.14159; if (($three == $three) and

   ($four === $four) and
   ($three != $four) and
   ($three < $four) and
   ($three <= $four) and
   ($four >= $three) and
   ($three <= $three) and
   ($my_pi > $three) and
   ($my_pi <= $four))
print("Yes!
");

else

print("No?
");

?>

      </source>