PHP/Operator/Conditional Operator

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

Conditional Operator

   <source lang="html4strict">

<?php

 function is_odd()
 { 
   global $num;
echo("$num is an odd number
");
 }
 function is_even()
 { 
   global $num;
echo("$num is an even number
");
 }

?> <html>

<head>
 <title>Conditional Operator</title>
</head>
<body>
 
<?php
 $num = 57;
 ( $num % 2 != 0 ) ? is_odd() : is_even();
 $num = 44;
 ( $num % 2 != 0 ) ? is_odd() : is_even();
?>
</body>

</html>

 </source>
   
  


Conditional Statements

   <source lang="html4strict">

<?php

           $Age = 20;
           if ($Age < 18) {
                   print "You"re young - enjoy it!\n";
           } else {
                   print "You"re not under 18\n";
           }
           if ($Age >= 18 && $Age < 50) {
                   print "You"re in the prime of your life\n";
           } else {
                   print "You"re not in the prime of your life\n";
           }
           if ($Age >= 50) {
                   print "You can retire soon - hurrah!\n";
           } else {
                   print "You cannot retire soon :( ";
           }
   ?>
 
 </source>
   
  


Less-than and greater-than

   <source lang="html4strict">

<? $age = 8; if ($age > 17) {

   print "> 17";

} if ($age >= 65) {

   print ">= 65";

} $kelvin_temp = 0; if ($kelvin_temp < 20.3) {

   print "< 20.3";

} ?>

 </source>
   
  


The trinary operator

   <source lang="html4strict">

<? $first_num =2; $second_num = 1; $max_num = $first_num > $second_num ? $first_num : $second_num; echo($max_num); ?>

      </source>