Home > Article > Backend Development > PHP4 Practical Application Experience (5)_PHP Tutorial
Author: Sun Movement
PHP also provides you with a way to handle multiple possibilities - the "if-elseif-else" structure. A typical "if-elseif-else" structural statement will look like this:
-------------------------- -------------------------------------------------- ---
if (the first condition is correct)
{
do this!
}
elseif (the second condition is correct)
{
do this!
}
elseif (the third condition is correct)
{
do this!
}
... etc...
else
{
do this!
}
-------------------------- -------------------------------------------------- --
Here is an example of how to use it:
-------------------------------- --------------------------------------------------
< html>
< head>
< style type="text/css">
td {font-family: Arial;}
< /style>
< /head>
< body>
< font face="Arial" size="+2">
Amazing fortune cookie production Program
< /font>
< form method="GET" action="cookie.php">
< table cellspacing="5" cellpadding="5" border= "0">
< tr>
< td align="center">
Please select a date
< /td>
< td align= "right">
< select name="day">
< option value="Monday">Monday
< option value="Tuesday">Tuesday
< ; option value="Wednesday">Wednesday
< option value="Thursday">Thursday
< option value="Friday">Friday
< option value="Saturday"> ;Saturday
< option value="Sunday">Sunday
< /select>
< /td>
< /tr>
< tr>
< tr>
< td colspan="2" align="center">
< input type="submit" value="Click me!">
< /td>
< /tr>
< /table>
< /form>
< /body>
< /html>
------------------------------------------------ --------------------------------
You will see that this simple form allows you to select a day of the week . The actual processing is done by the submitted PHP script "cookie.php".
-------------------------------------------------- ------------------------------------
< ?
if ( $day == "Monday")
{
$fortune = "Don't make everything simple and effective when you can find ways to make it complicated and exciting.";
}
elseif ($day == "Tuesday")
{
$fortune = "Life is a bridge to the game? - You must have some kind of trick up your sleeve.";
}
elseif ($day = = "Wednesday")
{
$fortune = "What can keep a sane person from going crazy in this world?";
}
elseif ($day == "Thursday" )
{
$fortune = "Don't be crazy, be fun";
}
elseif ($day == "Friday")
{
$fortune = "Just follow the times Go, follow the trend, you will find that type is a devil when you get promoted ";
}
else
{
$fortune = "Sorry, closed on weekends";
}
?>
< html>
< head>
< basefont face="Arial">
< /head>
< body>
This is your lucky word:
< br>
< b>< ? echo $fortune; ? >< /b>
< /body>
< /html>
--------------------- -------------------------------------------------- ----------
In this case, we use control sentences to assign different lucky words to each day.
There is an important point to note here - when an "if" statement in the structure is found to be true, PHP will execute the corresponding code, ignoring the remaining "if" statements in the structure , immediately jump out of the "if-elseif-else" structure and execute the lines following the entire structure.