PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax:
switch(n)
{
            case1:
statement if n=case1;
break;
            case2:
            statement if n=case2;
break;
            case3:
            statement if n=case3;
break;
              ...
default:
statement if n is different from all labels;
}
Working: First you have a single expression “n” (most frequently a variable), that is evaluated once. The value of the expression is compared with the values for each case in the structure. If any match found, the block of code associated with will be executed. Use break to prevent the code from executing into the next case automatically. The default statements is used if no match is found. PHP Switch Statement Flow Chart Example:
<?php
$num=30;   
switch($num){   
case 10:   
echo("number is equals to 10");   
break;   
case 20:   
echo("number is equal to 20");   
break;   
case 30:   
echo("number is equal to 30");   
break;   
default:   
echo("number is not equal to 10, 20 or 30");   
}  
?>
Output:                
number is equal to 30
Example2:
<?php
$today = date("D");
switch($today){
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 "Not Found.";
break;
}
?>
Output:
Today is Thursday.
Example3:
<?php
$num = 1;
switch ($num)
 {
case 1: echo 'The number is 1!';
break;
case 2: echo 'The number is 2';
break;
default: echo 'Unknown number';
}
?>
Output:
The number is 1!
Operator Description Example
<  Less than ($age < 18)
>  Greater than ($age > 18)
<= Less than or equal to ($age <= 18)
>= Greater than or equal to ($age >= 18)
!= Not equal to ($age != 18)
== Equal to ($age == 18)
Example4:
<?php
$age = 21;
switch ($age)
{
case 17: echo 'You\'re not old enough to play GTA!';
break;
case 17: echo 'You\'re old enough to drive!';
break;
case 18: echo 'You\'re old enough to play GTA!';
break;
case ($age > 18): echo 'You\'re quite old!';
break;
default: echo 'Was that a number?';
}
?>
Output:
You're quite old!
Example5:
<?php
extract($_POST);
if(isset($save))
{
switch($ch)
{
case 'ADD(+)':
$res=$fn+$sn;
break;
case 'SUB(-)':
$res=$fn-$sn;
break;
case 'MUL(*)':
$res=$fn*$sn;
break;
case 'DIV(/)':
$res=$fn*$sn;
}
}
?>
<form method="post">
<table align="center">
<tr>
<th> Enter First Number</th>
<th><input type="number" name="fn" value="<?php  echo @$fn;?>"/></th>
</tr>
<tr>
<th> Enter Second Number</th>
<th><input type="number" name="sn" value="<?php  echo @$sn;?>"/></th>
</tr>
<tr>
<th>Select Your Choice</th>
<th>
<select name="ch">
<option>ADD(+)</option>
<option>SUB(-)</option>
<option>MUL(*)</option>
<option>DIV(/)</option>
</select>
</th>
</tr>
<tr>
<th>Your Result</th>
<th><input type="number" readonly="readonly" disabled="disabled"
value="<?php  echo @$res;?>"/></th>
</tr>
<tr>
<thcolspan="2">
<input type="submit" name="save" value="Show Result"/>
</th>
</tr>
</table>
</form>
Output: PHP Switch Statement example

Related Topics

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

PHP current() function

PHP current() function The current() function in PHP returns the current element in an array, which is initialized to the first element inserted into the array. Syntax current ( array $array )   Parameter array (required)– This parameter represents the input array or countable object. Return This...

1 minute read.

PHP array_diff_assoc() Function

PHP array_diff_assoc() Function The array_diff_assoc() Function in PHP compares the difference of arrays and returns an array containing all the values from array1 that are not present in any of the other arrays. Syntax array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) Parameter array1(required)- This parameter signifies...

1 minute read.

PHP is_null() Function

The is_null() function in PHP is used to  find whether the specified variable is a null or not. It returns a Boolean value true if the parameter var is null else it returns...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP array_intersect_ukey() function

The array_intersect_ukey () function in PHP is used to compute the intersection of arrays with the help of a callable function on the keys for comparison. It returns an array containing all the...

1 minute read.

PHP crypt() Function

PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] )  Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...

2 minutes read.

PHP array_fill_keys() Function

The array_fill_keys() function in PHP fills an array with the value of the specified value parameter, using the values of the specified keys array as keys. Syntax array_fill_keys ( array $keys , mixed $value ) Parameter keys(required)- This parameter signifies the input array of values that will be...

1 minute read.

PHP stristr() Function

PHP stristr() Function The stristr() function in PHP finds the first occurrence of a string and returns the matched substring. It is a case-insensitive function. Syntax stristr ( string $haystack , mixed $needle [, bool $before_needle ] ) Parameter haystack(required)- This parameter represents the input string. needle(required)- This parameter...

2 minutes read.

PHP array_intersect_uassoc() function

The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing...

1 minute read.

Composer Installation

Composer is an application-level package manager for the PHP Programming language. The development began in April 2011 and it was first released on March 1, 2012. Commands: require install update remove Installation:  Download the installer by visiting...

1 minute read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP strrrev() Function

PHP strrrev () Function The strrev() function in PHP reverses a string. Syntax strrev ( string $string ) Parameter string(required)- This parameter represents the input string to be reversed. Return This function returns the reversed string. Example 1 Output Initial string: Hello...

1 minute read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

1 minute read.

PHP var_dump() Function

PHP var_dump() Function The var_dump() function in PHP is used to dump the information about a variable. It displays the structured information (arrays and objects) about one or more expressions. This function is also effective...

1 minute read.