PHP/Operator/Conditional Operator

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

Conditional Operator

 
<?php 
  function is_odd()
  { 
    global $num;
    echo("$num is an odd number<hr>");
  }
  function is_even()
  { 
    global $num;
    echo("$num is an even number<hr>");
  }
?>
<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>



Conditional Statements

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



Less-than and greater-than

 
<?
$age = 8;
if ($age > 17) {
    print "> 17";
}
if ($age >= 65) {
    print ">= 65";
}
$kelvin_temp = 0;
if ($kelvin_temp < 20.3) {
    print "< 20.3";
} 
?>



The trinary operator

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