PHP Forms Ex. #3: If-Elseif-Else Construction
For this PHP exercise, you will use the same format as the previous exercise, requesting input in the first part, and responding in the second, through the magic of PHP's if-else statement. In the first section, give the user an input field and request that they enter a day of the week.
For the second section, you'll need the following poem:
Laugh on Monday, laugh for danger.
Laugh on Tuesday, kiss a stranger.
Laugh on Wednesday, laugh for a letter.
Laugh on Thursday, something better.
Laugh on Friday, laugh for sorrow.
Laugh on Saturday, joy tomorrow.
Using the else-elseif-else construction, set each line to output in response to the day the user inputs, with a general response for any input that is not in the poem.
 
 
Here's the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>If-Elseif-Else Days of the Week</title>
</head>
<body>
<h2>Days of the Week</h2>
<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])){
?>
<form method="post" action="yourscript.php">
Please enter a day of the week: <br />
<input type="text" name="day" />
<p />
<input type="submit" name="submit" value="Go" />
</form>
<?php
//If form submitted, process input.
}else{
//Retrieve string from post submission
$day = $_POST["day"];
if ($day == 'Monday'){
echo "Laugh on Monday, laugh for danger.";
} elseif ($day == 'Tuesday'){
echo "Laugh on Tuesday, kiss a stranger.";
} elseif ($day == 'Wednesday'){
echo "Laugh on Wednesday, laugh for a letter.";
} elseif ($day == 'Thursday'){
echo "Laugh on Thursday, something better.";
} elseif ($day == 'Friday'){
echo "Laugh on Friday, laugh for sorrow.";
} elseif ($day == 'Saturday'){
echo "Laugh on Saturday, joy tomorrow.";
} else {
echo "No information for that day.";
}
}
?>
</body>
</html>
See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.
Comments
Uppercase user input
I tidied up the CONTROL FLOW by storing the poem as a set variables.
FIXED a problem with the user input having to be CASE SENSITIVE...
($day == 'Tuesday') means that 'TUESDAY' or 'tuesday' won't work. Fixed it by using the 'strtolower()' function......
$day = $_POST ['day'];
$day2 = strtolower($day);
Am I correct to do that, is there a better way to accept both upper/lower case user input (without having to declare a second variable like I have).
THE(php)CODE:
polishing strings
hi,
where you used this:
i used this
strtolower lowercases the whole string, ucfirst capitalizes the result.
this way you use only a variable (you lose readability, though) and maximise your chances to catch typos.
bye, great site!
bye bye
Better solution
I think that there is no need for the If-Elseif-Else Construction.. It can simply be done like this:
[reno u can do it with only one variable ;)]
You have missed the point
Thanks for providing an alternate way to solve the puzzle. However, you have missed the point of the exercise. The exercise is intended to help people who are learning about if-elseif-else constructions practice using those concepts. Your solution doesn't do that, whereas reno's solution does, with some extra thrown in.
alternate solution
later i did with the same technique like RENO..
thanks guys .. .awesome site!!
<?php
?>
#Some Thought
well 1st of all thanx..this is a great site for learning php..
i am an ameture, so if i did any mistake pardon me!
i am just thinking.... as a webdeveloper why should we give an option to users to add the day in a text box.. cummon.. i mean everybody know there are seven days in a week. So i prefer a drop down menu.
<?php
{
?>
<?php
}
{
{
{
}
elseif
{
{
{
{
}
}
?>
I've thrown some silk on this code, its elegant =).
i'm tempted to just use "for" loop. Although it's easier on the eye, nested "if-elseif" is clumsy. But for the sake of it, here's my complete code. Try it!
<?php
Enter a week day:
{
}
?>