Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Forms Ex. #3: If-Elseif-Else Construction

Ex6

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.

 

Comments

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:

$day = $_POST ['day'];
$day2 = strtolower($day);  // THIS IS THE BIT I ADDED.
                           

$monday = "Laugh on Monday, laugh for danger.";
$tuesday = "Laugh on Tuesday, kiss a stranger.";
$wednesday = "Laugh on Wednesday, laugh for a letter.";
$thursday = "Laugh on Thursday, something better.";
$friday = "Laugh on Friday, laugh for sorrow.";
$saturday = "Laugh on Saturday, joy tomorrow.";
$sunday = "Sleep on Sunday - hangover!!!!!!!!";

if ($day2 == 'monday'){
echo $monday;
}else if($day2 == 'tuesday'){
echo $tuesday;
}else if($day2 == 'wednesday'){
echo $wednesday;
}else if($day2 == 'thursday'){
echo $thursday;
}else if($day2 == 'friday'){
echo $friday;
}else if($day2 == 'saturday'){
echo $saturday;
}else if($day2 == 'sunday'){
echo $sunday;
}else{
echo "You didn't enter a day. Please try again.";
}

hi,

where you used this:

  1. $day = $_POST ['day'];
  2. $day2 = strtolower($day);

i used this

  1. $day = ucfirst(strtolower($_POST['day']));

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

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 ;)]

PHP code:


			$day = $_POST['day'];
			$day = strtolower($day);
			$monday = "Laugh on Monday, laugh for danger.";
			$tuesday = "Laugh on Tuesday, kiss a stranger.";
			$wednesday = "Laugh on Wednesday, laugh for a letter.";
			$thursday = "Laugh on Thursday, something better.";
			$friday = "Laugh on Friday, laugh for sorrow.";
			$saturday = "Laugh on Saturday, joy tomorrow.";
			$sunday = "Sleep on Sunday - hangover!!!!!!!!";
			if (!isset($$day)){
				echo 'This is not a day';
			}
			else{
				echo $$day;				
			}

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.

later i did with the same technique like RENO..

thanks guys .. .awesome site!!

  1. <?php
  2. 	$day = ($_POST["day"]);
  3. 	$poem = array( "Monday" => "Laugh on Monday, laugh for danger.", "Tuesday" => "Laugh on Tuesday, kiss a stranger.", "Wednesday" => "Laugh on Wednesday, laugh for a letter.", "Thursday" => "Laugh on Thursday, something better.", "Friday" => "Laugh on Friday, laugh for sorrow.", "Saturday" => "Laugh on Saturday, joy tomorrow.");
  4. 	$poem = array_flip($poem);	
  5. 	$search = array_search($day, $poem);
  6. 	echo $search;
  7. ?>

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.

  1. <?php
  2.  
  3. if(!isset($_POST['submit']))
  4. {
  5. ?>
  6. <form action="if else else if.php" method="post">
  7. Which is your fav day?<br />
  8. <select name="day">
  9. <option>Monday</option>
  10. <option>Tuesday</option>
  11. <option>Wednesday</option>
  12. <option>Thursday</option>
  13. <option>Friday</option>
  14. <option>Saturday</option>
  15. <option>Sunday</option>
  16. </select>
  17. <input type="submit" name="submit" value="GO" />
  18. </form>
  19. <?php
  20. }
  21. else{
  22. $day=$_POST["day"];
  23.  
  24. 	if($day=='Monday')
  25. 	{
  26. 		echo "Laugh on Monday, laugh for danger.<br>";
  27. 	}elseif
  28. 	($day=='Tuesday')
  29. 	{
  30. 		echo "Laugh on Tuesday, kiss a stranger. <br>";
  31. 	}elseif
  32. 	($day=='Wednesday')
  33. 	{
  34. 		echo "Laugh on Wednesday, laugh for a letter. <br>";
  35. 	}
  36. 	elseif
  37. 	($day=='Thursday')
  38. 	{
  39. 		echo "Laugh on Thursday, something better. <br>";
  40. 	}elseif
  41. 	($day=='Friday')
  42. 	{
  43. 		echo "Laugh on Friday, laugh for sorrow. <br>";
  44. 	}elseif
  45. 	($day=='Saturday')
  46. 	{
  47. 		echo "Laugh on Saturday, joy tomorrow. <br>";
  48. 	}else
  49. 	{
  50. 		echo "No information is available for this day. <br>";
  51. 	}
  52. }
  53. ?>

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!

  1. <?php
  2. <form action="" method="post">
  3.   Enter a week day:
  4.   <input name="dayname" type="text" maxlength="100" />
  5.   <input type="submit" value="Check it!">
  6. </form>
  7.  
  8. $wks=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  9. $common="Laugh on";
  10.  
  11. if (isset($_POST["dayname"]) && !empty($_POST["dayname"]))
  12.   {
  13.   $users=strtoupper($_POST["dayname"]);
  14.  
  15.   if (strtoupper($wks[0]) == $users) 
  16.     echo "$common $wks[0], when everything's better.";
  17.   elseif (strtoupper($wks[1]) == $users) 
  18.     echo "$common $wks[1], laugh for danger.";
  19.   elseif (strtoupper($wks[2]) == $users) 
  20.     echo "$common $wks[2], kiss a stranger.";
  21.   elseif (strtoupper($wks[3]) == $users) 
  22.     echo "$common $wks[3], laugh for a letter.";
  23.   elseif (strtoupper($wks[4]) == $users) 
  24.      echo "$common $wks[4], something better.";
  25.   elseif (strtoupper($wks[5]) == $users) 
  26.     echo "$common $wks[5], laugh for sorrow.";
  27.   elseif (strtoupper($wks[6]) == $users) 
  28.     echo "$common $wks[6], joy tomorrow.";
  29.   else echo "Yeah right.. try again if you want.";
  30.   }
  31. else echo " ";
  32. ?>