Multiple selection
The I love you program allows to execute alternatively more instructions or blocks of instructions, depending on the value of a variable which can have different values.
Here it is how the I love you program works:
| code iloveyou.htm | code iloveyou.php |
| <form action="iloveyou.php" method=post> Select a language: <select name="language"> <option>English</option> <option>Italian</option> <option>Greek</option> <option>Romanian</option> <option>Polish</option> </select> <input type=submit value="translate!"> </form> |
<?php switch ($language) { case "English": echo"I LOVE YOU"; break; case "Italian": echo"TI AMO"; break; case "Romanian": echo"TE IUBESC"; break; case "Polish": echo"KOCHAM CIE"; break; case "Greek": echo"S'ACHAPO"; break; default: break; }; ?> |
| The HTML page accepts a variable called language which can have different values: "English", "Italian" ... depending on the user's choice. | Look at the syntax of the SWITCH instruction. For each value of the $language variable the program executes a different instruction (or group of instructions). Break stops the execution and jumps to the end. Default (optional) says what to do if the variable has other unexpected values. |
How the IF instruction can become a crossroads of three roads?If the choice isn't too much complicated (as three roads) we can modify a little the IF instruction. Remember! IF is always a cross of two roads. But one of the roads in its turn can bifurcate. Practically the instruction which follows else is a second IF |
|