![]() |
|
PHP Switch Statement. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | PHP Switch Statement |
|
|
switch (expression) { case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; } This is how it works: • The expression written inside the switch statement i.e. switch(expression) is evaluated. This could also be a variable. • The value of the expression is compared with the values of each of the case labels. • If there is a match, the code associated with that case is executed. • After the code of the match case is executed, break is used to stop the code from running into the next case. • The default statement is used if a matching case is not found. Example <html> <body> <?php $exp = 2; switch ($exp) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3";
Next: PHP Defining Functions 1
|