SoFunction
Updated on 2025-04-11

PHP4 practical application experience (5)

Author: Sun Sports

PHP also provides you with a way to deal with multiple possible ways - "if-elseif-else" structure. A typical "if-elseif-else" structure 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 Lucky Cookie Production Program
< /font>

< form method="GET" action="">
< 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 choose a day of the week. The real processing is done by the submitted PHP script "".

--------------------------------------------------------------------------------
< ?

if ($day == "Monday")
{
$fortune = "When you can find a way to make everything complicated and exciting, don't make it simple and effective.";
}
elseif ($day == "Tuesday")
{
$fortune = "Life is the bridge to the game?-You must have used some trick.";
}
elseif ($day == "Wednesday")
{
$fortune = "What can make a clear mind live in this world and never go crazy?";
}
elseif ($day == "Thursday")
{
$fortune = "Don't be crazy, be fun";
}
elseif ($day == "Friday")
{
$fortune = "Just follow the times and follow the trend, and when you get promoted you will find that the type is a devil.";
}
else
{
$fortune = "Sorry, the weekend is closed";
}

?>

< html>
< head>
< basefont face="Arial">
< /head>

< body>
This is your lucky statement:
< 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 worth noting here - when a "if" statement in the structure is found to be true, PHP will execute the corresponding code, ignore the "if" statement in the remaining structure, immediately jump out of the "if-elseif-else" structure, and execute the lines behind the entire structure.