CodesJava

Easy learning with example program codes

PHP Switch


PHP Switch:

PHP Switch statement is a conditional statement, which is used to choose one of the action to perform from the multiple actions, relative to the true condition.

Syntax:

switch (expression) 
{
case value1:
    	code to be executed if expression=value1;
    	break;
 
case value2:
  	code to be executed if expression=value2;
        break;
 
case value3:
  	code to be executed if expression=value3;
        break;
…
 
default:
    	code to be executed if expression is different from all values;
}

Example

<!DOCTYPE html>
<html>
<body>
 
<?php
$car = "Hyundai";
 
switch ($car) {
    case "Honda":
        echo "Your favorite car is Honda!";
        break;
    case "BMW":
        echo "Your favorite car is BMW!";
        break;
    case "Hyundai":
        echo "Your favorite car is Hyundai!";
        break;
    default:
        echo "Your favorite car is neither Honda, BMW, nor Hyundai!";
}
?>
 
</body>
</html>

Output

Your favorite car is Hyundai!
Posted in PHP   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap