- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP switch statement
The PHP "switch" statement is a control structure that executes distinct blocks of code based on the value of a single expression.
PHP switch statement syntax
The syntax, or the general form, of the PHP "switch" statement is:
switch (expression) { case value1: // block of code to execute, if "expression" returns "value1" break; case value2: // block of code to execute, if "expression" returns "value2" break; case value3: // block of code to execute, if "expression" returns "value3" break; . . . case valueN: // block of code to execute, if "expression" returns "valueN" break; default: // block of code to execute if "expression" returns a value // that does not match with any cases break; }
The "switch (expression)" is the start of the switch statement, which evaluates the expression to determine which code block should be executed. Expressions can be any type of data that can be compared, such as variables or function calls.
The "break;" statement is used to end each case. It instructs PHP to exit the switch statement and proceed to the next line of code after executing the case-specific code block. If you omit this statement, the switch statement will continue to execute the code blocks of all subsequent cases, even if they do not match the expression.
The switch statement's default case. If none of the specified cases match the expression, this code block is executed. It is not necessary to include a default case in a switch statement, but it is generally recommended for handling unexpected values.
PHP switch statement example
Consider the following PHP code as an example of the "switch" statement:
<?php $x = 4; switch($x) { case 1: echo "The value of \$x is 1"; break; case 2: echo "The value of \$x is 2"; break; case 3: echo "The value of \$x is 3"; break; case 4: echo "The value of \$x is 4"; break; case 5: echo "The value of \$x is 5"; break; case 6: echo "The value of \$x is 6"; break; case 7: echo "The value of \$x is 7"; break; case 8: echo "The value of \$x is 8"; break; default: echo "The value of \$x is unknown"; } ?>
The output produced by the above PHP example is shown in the snapshot given below:
That is, the code inside the () bracket of a "switch" is considered a single expression and will be evaluated once. Therefore, $x is evaluated, and the value after evaluation will be 4, as the value of $x is 4. Now this four gets compared with each and every case in the structure. Since 4 matches with case 4, therefore, the block of code associated with this case will be executed.
Note: The "break" keyword or statement is used to skip the execution of remaining cases.
Note: The "default" case is used to execute some block of code if no match is found.
We can use a "switch" statement to replace if...elseif...else.
Here is another example demonstrating the "switch" statement in PHP.
<?php $day = date("D"); switch ($day) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "What! are you an alien?"; } ?>
Whatever the current day is when executing this PHP code, it will be printed on the output.
Advantages of the switch statement in PHP
- Clarity: The switch statement simplifies code condition handling. It simplifies code, especially if there are many conditions to check.
- Switch statements are more efficient than if/else statements. Instead of checking each condition, it evaluates the expression once and jumps to the matching case.
- Easy to update: New conditions can be added as cases in the switch statement, making code updates simple.
Disadvantages of the switch statement in PHP
- PHP's switch statement only checks for expression-case value equality. It cannot operate under complex conditions.
- The switch statement doesn't support logical operators like "and" and "or". This makes complex code conditions difficult.
- PHP's switch statement can act unexpectedly with variables of different data types, especially if type juggling is involved. Errors or unexpected results may result.
- No fall-through protection: If the "break" statement is omitted, execution will continue to the next case even if it doesn't match the expression. Unexpected results and hard-to-find code bugs can result.
« Previous Tutorial Next Tutorial »