PHP switch Statement

The switch statement in PHP is a control structure used to execute different blocks of code based on the value of a variable or expression. It is similar to a series of if...elseif...else statements but is often used when you want to compare the same variable or expression against multiple possible values.


Syntax of the switch Statement


switch (expression) {
    case value1:
        // Code to be executed if expression equals value1
        break;
    case value2:
        // Code to be executed if expression equals value2
        break;
    case value3:
        // Code to be executed if expression equals value3
        break;
    default:
        // Code to be executed if none of the cases match
}


Explanation:


  • switch (expression): This is the value or expression to be compared.
  • case value:: If the value of expression matches value, the associated block of code is executed.
  • break;: This exits the switch block after a matching case is found and its code has been executed, preventing subsequent case blocks from running.
  • default:: This block executes if none of the case values match the expression.
Example
<!DOCTYPE html>
<html>
<body>
<?php
$day = "Tuesday";

switch ($day) {
case "Monday":
echo "Today is Monday.";
break;
case "Tuesday":
echo "Today is Tuesday.";
break;
case "Wednesday":
echo "Today is Wednesday.";
break;
default:
echo "Not a valid day.";
}
?>
</body>
</html>

Try it yourself

The break Keyword


  • break; is used to exit a loop or switch block immediately.
  • It is commonly used in loops and switch statements to prevent further execution once a condition is met.

Warning

Without break;, PHP will continue to execute the next case statements (known as "fall-through" behavior).

Example of without break keyword
<!DOCTYPE html>
<html>
<body>
<?php
$day = "Tuesday";

switch ($day) {
case "Monday":
echo "Today is Monday.";
break;
case "Tuesday":
echo "Today is Tuesday.";
case "Wednesday":
echo "Today is Wednesday.";
break;
default:
echo "Not a valid day.";
}
?>
</body>
</html>

Try it yourself

The default Keyword


The default keyword in PHP is used in a switch statement to specify the block of code that should be executed if none of the case values match the evaluated expression.


Example of Using default in a switch Statement
<!DOCTYPE html>
<html>
<body>

<?php
$day = "Saturday";

switch ($day) {
case "Monday":
echo "It's Monday.";
break;
case "Tuesday":
echo "It's Tuesday.";
break;
case "Wednesday":
echo "It's Wednesday.";
break;
default:
echo "It's not Monday, Tuesday, or Wednesday.";
}
?>


</body>
</html>

Try it yourself

default Placed Elsewhere


Although not common practice, the default block can be placed anywhere within a switch structure. PHP will execute the default block if no matching case values are found, regardless of its position.

Example
<!DOCTYPE html>
<html>
<body>

<?php
$day = "Monday";

switch ($day) {
default:
echo "It's another day.";
break;
case "Monday":
echo "It's Monday.";
break;
case "Tuesday":
echo "It's Tuesday.";
break;
}
?>

</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.