PHP lessons

Multiple selection

I LOVE YOU in all the languages of the world

Select a language:

What can we do if the crossroads is of three or four roads?

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.

The multiple selection instruction is SWITCH; it allows many options that can be hardly managed by the IF instruction.

Here it is how the I love you program works:

code iloveyou.htmcode iloveyou.php
<form action="iloveyou.php" method=post>
Select a language:
<select name="language">
<input type=submit value="translate!">
switch ($language) {
case "English":
echo"I LOVE YOU";
case "Italian":
echo"TI AMO";
case "Romanian":
echo"TE IUBESC";
case "Polish":
case "Greek":
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

ifcomposto (13K)

Valid HTML 4.01 Transitional