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

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

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

Comparing numbers and strings

 
<?
// These values are compared using dictionary order
if ("x54321" > "x5678") {
    print "The string "x54321" is greater than the string "x5678".";
} else {
    print "The string "x54321" is not greater than the string "x5678".";
}



Comparison Operators

 
example         name                               result
$a == $b        Equal                              True if $a is equal to $b.
$a === $b       Identical                          True if $a is equal to $b, and they are of the same type. (PHP4 only)
$a != $b        Not equal                          True if $a is not equal to $b.
$a < $b         Less than                          True if $a is strictly less than $b.
$a > $b         Greater than                       True if $a is strictly greater than $b.
$a <= $b        Less than or equal to              True if $a is less than or equal to $b.
$a >= $b        Greater than or equal to           True if $a is greater than or equal to $b.

<?php 
  $a = ("PHP"=="PHP")? "true":"false";
  $b = ("PHP"=="PERL")?"true":"false";
  $c = (1.785==1.785)? "true":"false";
  $d = (5 != 5)?"true":"false";
  $e = (true == true)?"true":"false";
  $f = (false != false)?"true":"false";
  $g = (100<200)?"true":"false";
  $h = (100<100)?"true":"false";
  $i = (100<=100)?"true":"false";
  $j = ( -1 > 1 )?"true":"false";
  
  $result =  "TEST STRINGS \$a:$a  \$b:$b<br>";
  $result .= "TEST NUMBERS \$c:$c \$d:$d<br>";
  $result .= "TEST BOOLEANS \$e:$e \$f:$f<br>";
  $result .= "TEST LESS THAN \$g:$g \$h:$h<br>";
  $result .= "TEST LESS THAN OR EQUAL \$i:$i<br>";  
  $result .= "TEST GREATER THAN \$j:$j";
?>
<html>
 <head>
  <title>Comparison Operators</title>
 </head>
 <body>
  <?php echo( $result ); ?>
 </body>
</html>



Equality Operators in action

<html>
<head>
   <title>Equality</title>
</head>
<body>
     <?php
          $a = 1;
          $b = "1";
   
          print("(\$a == \$b)" . (($a == $b) ? "True" : "False") . "<br />");
          print("(\$a != \$b)" . (($a != $b) ? "True" : "False") . "<br />");
          print("(\$a === \$b)" . (($a === $b) ? "True" : "False") . "<br />");
          print("(\$a !== \$b)" . (($a !== $b) ? "True" : "False") . "<br />");
     ?>
</body>
</html>



Equality Operators summary table

 
<!--
Operation        Operator          Example
Equality         ==                $a == $b
Inequality       !=                $a != $b
Inequality       <>                $a <> $b
Identity         ===               $a === $b
Nonidentity      !==               $a !== $b 
-->



Loose Comparison Operators

 
Example         Name                            Description 
$a == $b        Equal to                        True if $ais equal to $b 
$a != $b        Not equal to                    True if $ais not equal to $b 
$a < $b         Less than                       True if $ais less than $b 
$a > $b         Greater than                    True if $ais greater than $b 
$a <= $b        Less than or equal to           True if $ais less than or equal to $b 
$a >= $b        Greater than or equal to        True if $a is greater than or equal to $b 
$a === $b       Equal to                        True if $ais equal to $b and they are of the same type
$a !== $b       Not equal to                    True if $ais not equal to $bor they are not of the same type



The equality operator

 
<?
$new_messages = 10
if ($new_messages == 10) {
    print "You have ten new messages.";
}
if ($new_messages == $max_messages) {
    print "You have the maximum number of messages.";
}

if ($dinner == "B") {
    print "B.";
}
?>



These values are compared using dictionary order

 
if ("6 p" < "55 c") {
    print ""6 p" is less than than the string "55 c".";
} else {
    print ""6 p" is not less than the string "55 c".";
}



These values are compared using numeric order

 
if ("6 p" < 55) {
    print ""6 p" is less than the number 55.";
} else {
    print ""6 p" is not less than the number 55.";
}
?>



The Ternary Operator

 
<?
 $age = 18;
 $agestr = ($age < 16) ? "child" : "adult";
?>



Using Comparison Operators in if Statements

 
<?php
    $p = 14;
    if ($p < 15) {
        echo "Sorry! <BR>";
    } else {
        echo "Congratulations";
    }
?>