Page 95 - Beginning PHP 5.3
P. 95

Chapter 4: Decisions and Loops
                           The ternary operator can be thought of as a compact version of the  if...else  construct. The preceding
                          code reads as follows: If   expression1  evaluates to  true , the overall expression equals  expression2 ;
                         otherwise, the overall expression equals   expression3 .

                           Here ’ s a  “ real world ”  example to make this concept clearer:
                             $widgets = 23;
                             $plenty = “We have plenty of widgets in stock.”;
                             $few = “Less than 10 widgets left. Time to order some more!”;

                             echo ( $widgets  > = 10 ) ? $plenty : $few;
                           This code is functionally equivalent to the example in the  else  statement section earlier in this chapter.
                          Here ’ s how it works.

                            Three variables are created: the   $widgets  variable, with a value of  23 , and two variables,  $plenty  and
                            $few , to hold text strings to display to the user. Finally, the ternary operator is used to display the
                         appropriate message. The expression   $widgets  > = 10  is tested; if it ’ s  true  (as it will be in this case),
                         the overall expression evaluates to the value of   $plenty . If the test expression happens to be  false , the
                         overall expression will take on the value of   $few  instead. Finally, the overall expression  —  the result of
                          the ternary operator  —  is displayed to the user using   echo() .
                           Code that uses the ternary operator can be hard to read, especially if you ’ re not used to seeing the
                         operator. However, it ’ s a great way to write compact code if you just need to make a simple   if...else
                         type of decision.


                       Try It Out     Use Decisions to Display a Greeting
                         Here’s a simple example that demonstrates the if, elseif, and else statements, as well as the ?
                         (ternary) operator. Save the script as greeting.php in your document root folder.
                             This script (and most of the other scripts in this book) link to the common.css style sheet file listed in
                             Chapter 2, so make sure you have common.css in your document root folder too.

                             <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                             “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                             <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                               <head>
                                 <title>Greetings</title>
                                 <link rel=”stylesheet” type=”text/css” href=”common.css” />
                               </head>
                               <body>
                             <?php
                             $hour = date( “G” );
                             $year = date( “Y” );
                             if ( $hour >= 5 && $hour < 12 ) {
                               echo “<h1>Good morning!</h1>”;
                             } elseif ( $hour >= 12 && $hour < 18 ) {
                               echo “<h1>Good afternoon!</h1>”;
                             } elseif ( $hour >= 18 && $hour < 22 ) {



                                                                                                          57





                                                                                                      9/21/09   8:52:08 AM
          c04.indd   57                                                                               9/21/09   8:52:08 AM
          c04.indd   57
   90   91   92   93   94   95   96   97   98   99   100