PHP/Operator/Comparison Operators

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

Comparing numbers and strings

   <source lang="html4strict">

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

}

 </source>
   
  


Comparison Operators

   <source lang="html4strict">

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
"; $result .= "TEST NUMBERS \$c:$c \$d:$d
"; $result .= "TEST BOOLEANS \$e:$e \$f:$f
"; $result .= "TEST LESS THAN \$g:$g \$h:$h
"; $result .= "TEST LESS THAN OR EQUAL \$i:$i
"; $result .= "TEST GREATER THAN \$j:$j";

?> <html>

<head>
 <title>Comparison Operators</title>
</head>
<body>
 <?php echo( $result ); ?>
</body>

</html>

 </source>
   
  


Equality Operators in action

   <source lang="html4strict">


<html> <head>

  <title>Equality</title>

</head> <body>

    <?php
         $a = 1;
         $b = "1";
  
         print("(\$a == \$b)" . (($a == $b) ? "True" : "False") . "
"); print("(\$a != \$b)" . (($a != $b) ? "True" : "False") . "
"); print("(\$a === \$b)" . (($a === $b) ? "True" : "False") . "
"); print("(\$a !== \$b)" . (($a !== $b) ? "True" : "False") . "
");  ?>

</body> </html>

      </source>
   
  


Equality Operators summary table

   <source lang="html4strict">


      </source>
   
  


Loose Comparison Operators

   <source lang="html4strict">

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

 </source>
   
  


The equality operator

   <source lang="html4strict">

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

} ?>

 </source>
   
  


These values are compared using dictionary order

   <source lang="html4strict">

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

}

 </source>
   
  


These values are compared using numeric order

   <source lang="html4strict">

if ("6 p" < 55) {

   print ""6 p" is less than the number 55.";

} else {

   print ""6 p" is not less than the number 55.";

} ?>

 </source>
   
  


The Ternary Operator

   <source lang="html4strict">

<?

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

?>

 </source>
   
  


Using Comparison Operators in if Statements

   <source lang="html4strict">

<?php

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

?>

 </source>