php Switch statement

You can write more than one code block or statement in a switch statement, it works like an if statement, in it you use the break keyword after each statement, if you do not use this keyword, then all the content given in it is displayed in the web page. In addition to the break keyword true condition statement, it stops other content statements from being displayed on the web page, if the condition is false then the default content statement is displayed in the web page.

Syntax

switch(expression){

case value1:

code executed;

break;

case value2:

code executed;

break;

......

default:

code executed (if above values are not matched);

}

Example 1

File name : index.php

<?php

$ num = 120;

switch($num){

case 10:

echo("$num = 10");

break;

case 20:

echo("$num = 20");

break;

case 30:

echo("$num = 30");

break;

default:

echo("$num is not equal to 10, 20 or 30");

}

?>

Output

120 is not equal to 10, 20 or 30


Connect with us

facebook logo
Email : contact@amcodestar.com
© Copyright amcodestar.com 2020 - 2023 All Rights Reserved.
About us     term and condition      comment policy
DMCA.com Protection Status