PHP Switch Statement Syntax
×

PHP Switch Statement

4525

PHP Switch Statement is used to select one block from many blocks. That one block of code will be executed. Break is used to stop the particular case. Case will run continuously until it finds a break.

The default block is use for print default case value, when all cases not match with switch value, in this condition default block will work.

Switch Statement Syntax:

switch (n value)
{
case label 1:
code to be executed if n value = label 1;
break;
case label 2:
code to be executed if n value = label 2;
break;
case label 3:
code to be executed if n value = label 3;
break;
case label 4:
code to be executed if n value = label 4;
break;
default:
code to be executed if n value is different from all labels;
}


PHP Switch Statement Example:

<?php

$x = 20;
switch ($x) {
case 10:
echo "1st case is ok";
break;

case 20:
echo "2nd case is ok";
break;

case 30:
echo "3rd case is ok";
break;

case 40:
echo "4th case is ok";
break;

default:
echo "Your $x value is not equal to any case";

?>

Result: 2nd case is ok



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments