What is PHP Switch…Case Statements
Check Several Possible constant values for an expression.
PHP If…Else Vs Switch…Case
The switch-case statement is an alternative to the if-elseif-else statement, which does almost the same thing. The switch-case statement tests a variable against a series of values until it finds a match, and then executes the block of code corresponding to that match.
Syntax:-
Consider the following example, which displays a different message for each day.
Example:-
The switch-case
statement differs from the if-elseif-else
statement in one important way. The switch
statement executes line by line (i.e. statement by statement) and once PHP finds a case
statement that evaluates to true, it’s not only executes the code corresponding to that case statement, but also executes all the subsequent case
statements till the end of the switch
block automatically.
This Example for Without Break:-
This Example for different case to same Statements
This Example for one case two Statements
This Example For variable not match so print is default
To prevent this add a break
statement to the end of each case
block. The break
statement tells PHP to break out of the switch-case
statement block once it executes the code associated with the first true case.
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND