PHP/Operator/Logical Operators — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 07:06, 26 мая 2010

Greate and equal

<?
$a = 21;
$b = 15;
echo "<P>Original value of \$a is $a and \$b is $b</P>";
if ($a >= $b) {
    echo "<P>TEST\$a is greater than or equal to \$b</P>";
} else {
    echo "<P>TEST\$a is not greater than or equal to \$b</P>";
}
?>



Greater than

<?
$a = 21;
$b = 15;
echo "<P>Original value of \$a is $a and \$b is $b</P>";
if ($a > $b) {
    echo "<P>TEST \$a is greater than \$b</P>";
} else {
    echo "<P>TEST \$a is not greater than \$b</P>";
}
?>



Leap Year Determination

 
<?php
function is_leap_year($year) {
    return ((($y % 4) == 0) && ((($y % 100) != 0) || (($y % 400) == 0)));
}
foreach (range(1999, 2009) as $year) {
    echo "<p>{$year} = ", is_leap_year($year) ? "Leap Year" : "not", "</p>";
}
?>



Less than

<?
$a = 21;
$b = 15;
echo "<P>Original value of \$a is $a and \$b is $b</P>";
if ($a < $b) {
    echo "<P>TEST\$a is less than \$b</P>";
} else {
    echo "<P>TEST\$a is not less than \$b</P>";
}
?>



Less than and equal

<?
$a = 21;
$b = 15;
echo "<P>Original value of \$a is $a and \$b is $b</P>";
if ($a <= $b) {
    echo "<P>TEST\$a is less than or equal to \$b</P>";
} else {
    echo "<P>TEST\$a is not less than or equal to \$b</P>";
}
?>



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.";
}
?>



Logical Operators in Order of Precedence

 
<!--
Operation          Operator        Class         Associativity       Example
---------------------------------------------------------------------------------
Logical AND        &&              Binary        Left                $a && $b
Logical OR         ||              Binary        Left                $a || $b
Logical NOT        !               Unary         Right               !$a
Logical XOR        xor             Binary        Left                $a xor $b
Logical AND        and             Binary        Left                $a and $b
Logical OR         or              Binary        Left                $a or $b 
-->



Logic Operators in PHP

 
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



Negation operator

 
$first_name = "first";
$last_name = "last";
<?
if (! strcasecmp($first_name,$last_name)) {
    print "$first_name and $last_name are equal.";
}
?>



Not equal: !=

<?
$a = 21;
$b = 15;
echo "<P>Original value of \$a is $a and \$b is $b</P>";

if ($a != $b) {
    echo "<P>TEST\$a is not equal to \$b</P>";
} else {
    echo "<P>TEST\$a is equal to \$b</P>";
}
?>



Or operator

 
<?php 
$user1 = "Paul"; 
$user2 = "Donna"; 
if ($username == $user1 || $username == $user2) 
{ 
    print "$username welcome."; 
} 
?>



Relational Operators in action

<html>
<head>
   <title>Relational Operators</title>
</head>
<body>
     <?php
        print("(5 < 5) is "  . ((5 < 5) ? "True" : "False") . "<br />");
        print("(5 <= 5) is "  . ((5 <= 5) ? "True" : "False") . "<br />");
        print("(5 > 5) is "  . ((5 > 5) ? "True" : "False") . "<br />");
        print("(5 >= 5) is "  . ((5 >= 5) ? "True" : "False") . "<br />");
     ?>
</body>
</html>



Relational Operators summary table

<!--

Operation       Operator            Example
---------------------------------------------------------
Less than       <                   $a < $b
Less than       <=                  $a <= $b
or equal to
Greater than    >                   $a > $b
Greater than    >=                  $a >= $b 
or equal to
-->



The not-equals operator

 
<?
$new_messages = 10

if ($new_messages != 10) {
    print "You don"t have ten new messages.";
}
if ($dinner != "B") {
    print "not b";
}
?>



Use the boolean compare operators

<?
$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!<BR>");
else
 print("No?<BR>");
?>