saftutorials.com
PHP Switch Tutorial

You can execute one of many parts of code by using a switch statement in PHP. A switch statement is used for avoiding the long block of codes.

Why use Switch Statement?
Switch Statement can execute one of many parts of code by using in PHP. Switch statement is used for avoiding long block of codes.

Syntax of Switch statement

switch (expression){

case first_label:

code to be executed if expression = label1;

break;  

case second_label:

code to be executed if expression = label2;

break;

default:

code to be executed if the expression is different from both first_label and second_label;

}


Example of Switch statement

<!DOCTYPE html>
<html>
<body>
<?php
$variable = 'float'; 
switch ($variable){
case "integer":
$value = "This is integer type";
break;
case "string":
$value = "This is string type";
break;
case "float":
$value = "This is float type";
break;
default:
$value = "This is other type";
}
?>

<p>one statement will be correct : <?php echo $value;?></p>
</body>
</html>

The above code produces the following results

Outputs: one statement will be correct: This is float type


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic