PHP Switch Statement

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 used to print the default case value, when all cases don’t match with switch value, in this condition default block will work.

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;
}


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




Coding Tag

Coding Tag always happy to serve you in Information Technology (IT). Learn HTML, CSS, Javascript, PHP, Android, Digital Marketing, Latest IT News and many more.

Leave a Reply

avatar
  Subscribe  
Notify of