You get a bonus - 1 coin for daily activity. Now you have 1 coin

PHP selection constructs

Lecture



Often, instead of several consecutive if-else instructions, it is advisable to use a special switch-case selection construct. This construction is intended to select actions, depending on the value of the specified expression. The switch-case construct is somewhat similar to the if-else construct , which, in essence, is its counterpart. The choice design can be used if there are many prospective options, for example, more than 5, and for each option specific actions must be performed. In this case, using the if-else construct becomes really inconvenient.

The switch-case construction syntax is:

switch(выражение) {
case значение1: команды1; [break;]
case значение2: команды2; [break;]
. . .
case значениеN: командыN; [break;]
[default: команды_по_умолчанию; [break]]
}

The principle of operation of the switch-case construction is as follows:

  1. The value of the expression is calculated;
  2. A set of values ​​is viewed. Let value1 be equal to the value of the expression calculated in the first step. If the construction (operator) break is not specified, then the commands i, i + 1, i + 2, ..., N will be executed. Otherwise (there is a break) only the command with the number i will be executed.
  3. If none of the values ​​in the set matches the value of the expression, then the default block is executed if it is specified.

Let us give examples of using the switch-case construction:

<? php
$ x = 1 ;
// Используем if-else
if   ($ x ==   0 )   {
echo "x=0<br>" ;
}   elseif   ($ x ==   1 )   {
echo "x=1<br>" ;
}   elseif   ($ x ==   2 )   {
echo "x=2<br>" ;
}
// Используем switch-case
switch   ($ x )   {
case   0 :
echo "x=0<br>" ;
     break;
case   1 :
echo "x=1<br>" ;
     break;
case   2 :
echo "x=2<br>" ;
     break;
}
?>

This script displays x = 1 twice. Another example of using the switch-case construct:

<? php
$ x = "Яблоко" ;
switch   ($ x )   {
case   "Яблоко" :
echo "Это Яблоко" ;
     break;
case   "Груша" :
echo "Это Груша" ;
     break;
case   "Арбуз" :
echo "Это Арбуз" ;
     break;
}
?>

This script displays "This is an Apple."

The switch construction is carried out in stages. No code is executed first. Only when the case construct is found with a value that matches the value of the switch statement , does PHP begin to execute the construct. PHP continues to execute constructions until the end of the switch block until the break statement is encountered. If you do not use break constructions (operators), the script will look like this:

<? php
$ x = 0 ;
switch   ($ x )   {
case   0 :
echo "x=0<br>" ;
case   1 :
echo "x=1<br>" ;
case   2 :
echo "x=2<br>" ;
}
// Без использования break выводит
// x=0
// x=1
// x=2
?>

The statement list for case can also be empty; it simply transfers control to the statement list to the following case construction:

<?php
switch ($x) {
case 0:
case 1:
case 2:
echo "x меньше, чем 3, но не отрицателен";
break;
case 3:
echo "x=3";
}
?>

When none of the values ​​in the set matches the value of the expression, then the default block is executed if it is specified, for example:

<? php
$ x = 3 ;
switch   ($ x )   {
case   0 :
echo "x=0" ;
     break;
case   1 :
echo "x=1" ;
     break;
case   2 :
echo "x=2" ;
     break;
default:
echo "x не равен 0, 1 или 2" ;
}
?>

This script displays "x is not equal to 0, 1 or 2" , because the variable $ x = 3 .

The switch-case also has an alternative syntax:

switch(выражение):
case значение1: команды1; [break;]
. . .
case значениеN: командыN; [break;]
[default: команды_по_умолчанию; [break]]
endswitch;

A practical example of using alternative syntax for a switch-case construct:

<? php
$ x = 3 ;
switch   ($ x ):
case   0 :
echo "x=0" ;
     break;
case   1 :
echo "x=1" ;
     break;
case   2 :
echo "x=2" ;
     break;
default:
echo "x не равен 0, 1 или 2" ;
endswitch;
?>

As you already understood, this script displays "x is not equal to 0, 1 or 2" , since $ x = 3 .


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)