DEV Community

Cover image for How PHP Switch Case Statement
CodeExampler
CodeExampler

Posted on

How PHP Switch Case Statement

The PHP switch Case statement is followed by the variable or value to compare

The case statements inside the switch block are followed by the alternate variable or value to compare

The Code following the colon in the case statement will only execute if the switch and case values match

Only one dereliction case is allowed, which will execute if no break statement exists previous

a,The break statement prevents any following cases from being tested, therefore precluding their law from running
b, The continue statement can be used in place of break
continue accepts an voluntary numeric argument which tells it how numerous situations of enclosing circles it should skip – see latterly in the composition for what this means in switch statements

Switch/ case makes a loose comparison by dereliction
This means that 2 and “ 2” are equal – as are 0 and FALSE – as the variable type is ignored
See latterly in the composition on how to use strict comparisons where the variable type are considered

Switch Case PHP Example

<?php

$i=250;
Switch($i)
{
Case 150:
echo "Value is 150";
Break;

Case 250:
echo "Value is 250";
Break;

Case 350:
echo "Value is 350";
Break;

Case 450:
echo "Value is 450";
Break;

Default:
echo "Enter Correct Value";
Break;
}

?>

Learn also PHP Variables Tutorials for Beginners because wihthout good knowledge on basic variables you don't understand PHP Switch Case by CodeExampler Website

Switch Case PHP Free tutorial for Beginners by CodeExampler

Top comments (1)

Collapse
 
codeexamplerlearn profile image
CodeExampler • Edited

How to Comment in Javascript by CodeExampler for Free