Similar to the if-else conditional is the if-elseif (or if-elseif-else ). It acts like a running if statement and can be expanded to whatever length you require. if (conditional) { statement(s); } elseif (conditional2) { statement(s)2; } Here's another example: if (conditional) { statement(s); } elseif (conditional2) { statement(s)2; } else { statement(s)3; } You'll create a new hello.php page similar to the one from Chapter 3, HTML Forms and PHP, which utilizes an if-elseif conditional and the date() function to write a customized greeting to the user . To use elseif: -
Create a new PHP document in your text editor. -
Write the standard HTML header and open the PHP section of the page (Script 6.6). <HTML><HEAD><TITLE>If-elseif Conditionals</TITLE><BODY> <?php Script 6.6. You have nested one if-elseif-else within another if-else conditional which is perfectly acceptable as long as you maintain your syntax (continuing to indent subsequent lines helps). -
Create the main if conditional. if ($Username) { As a matter of good form, you'll only print this greeting if you have the username. -
print ("Good "); You'll set up the code to print the first part of the greeting separately, rather than along with each of the three following specific salutations. This way, if you want to later change the specific wording, you only need to make alterations in one place. -
if (date("A") == "AM") { The date() function is used to determine any date specific informationday of the week, month, etc.based upon the parameter it is fed. Here, date("A") will return either "AM" or "PM". The date() function will be covered in more detail in Chapter 13, Creating Web Applications. -
print ("morning, "); } elseif ( ( date("H") >= 12 ) and ( date("H") < 18 ) ) { Date("H") returns the hour of the day in military format. So if it is between noon and 6 p.m., you'll say "Good afternoon." -
print ("afternoon, "); } else { print ("evening, "); If it is not the morning or the afternoon, it must be the evening, so you'll use else as a catch-all. -
} // Close the date if. print ("$Username"); print ("!\n"); } else { print ("Please log in.\n"); If you do not have a username, you'll request that the user log in before proceeding. -
} // Close the username if. The comments help you keep track of complicated and nested if conditionals, helping to insure that you close your if conditionals properly. -
Save your script as hello.php, upload it to the server, and test it in your Web browser (Figures 6.8 and 6.9). Figure 6.8. The page greets you like so if it has the $Username value and the time on the server is between 6 p.m. midnight. Figure 6.9. This is the greeting the page gives you assuming it receives the $Username and it is between noon and 6 p.m. Tip You should always put the else as the last part of a conditional since it will be executed unless one of the conditions to that point has been met. Tip You can continue to use elseif s as many times as you want as part of one if conditional. Tip PHP also allows you to write elseif as two words if you would prefer to do so: if (condition) { statement(s); } else if (condition2) { statements(2); } Tip The date() function is very useful but it only reflects the time on the server, not the time where the user is. (In fact, if the time on the server is incorrect, the date() function will reflect the incorrect time and date.) |