PHP Switch Statement

Switch Statement in PHP

PHP-Switch-Statement.jpg
PHP Switch Statement

The switch statement is used to execute various actions based on different conditions.

Key points about switch case to be noticed:

  • The switch statement can have one default only. Over one default may result in a fatal error.
  • Each case may have a statement of the break, which is used to terminate the statement sequence.
  • PHP allows you to use any number, character, string and switch expression function.
  • you can use semicolon(;), colon(:). it will not generate any error

syntax of a switch statement in PHP

switch (n) {
    case label1:
        code to be executed if n=label1;
        break;
    case label2:
        code to be executed if n=label2;
        break;
    case label3:
        code to be executed if n=label3;
        break;
    ...
    default:
        code to be executed if n is not similar from all labels;
}

It works like this: In a switch statement in Php, we have first a single expression n (most often a variable), which is evaluated once. Then, the expression value is compared with the values in the structure for each case. If a match occurs, the code block corresponding to that case will be executed. Use the break to automatically stop the code from running into the next case. If no match is found, then the default statement is used.

Flow chart of a switch statement in PHP
flow-chart-switch-statement.jpg
Read more...

Also, Visit here - MySQL Tutorial for Beginners

Comments

Popular posts from this blog

Laravel Tutorial for Beginners

Ajax Tutorial

CodeIgniter Tutorial