PHP and MySQL (56 Blogs) Become a Certified Professional

All you Need to Know about Switch Case in PHP

Published on Sep 09,2019 718 Views


Switch case is something that cannot be missed if we talk about any programming language. The following pointers will be covered in this Switch Case in PHP article,

 

What is a Switch Case?

In this article, we are going to talk about something called switch case in PHP and the basic idea behind the switch case is that we are going to do something different inside the browser depending on some kind of answer we might have. It is an alternative to a series of if statement, which does almost the same thing.

switch-case-in-phpA Variable is tested by switch-case statements against a series of values until it finds a match, and then executes the block of code corresponding to that match.

 

Moving on with this article on switch case in php

Code: Switch case in PHP

In our example, the answer is going to be a value assigned to some variable. So the way we write out a switch is by
switch()
{
}
though it looks similar to if statement, it is not exactly same though because in an if statement, you do actually have different conditions where in switch, you write all the different possible answers inside the curly brackets itself. So let’s go ahead and write out one different case

<?php
$x=1;
switch($x)
{
case 1:
echo "This answer is case 1";
break;
}

Example- switch case in php- Edureka

We can also assign string to the variable.

<?php
$x="number";
switch($x)
{
case 1:
echo "This answer is case 1";
break;
case "number";
echo "This answer is case number";
break;
}

Example- switch case in php- Edureka

Basically, we use break to prevent the code from running into the next case automatically. We use default statement, if no match is found.

<?php 
$company = "amazon"; 
switch($company) 
{ 
case "simplilearn": echo "Company choosed: simplilearn"; 
break; 
case "edureka": echo "Company choosed: edureka";
break; 
case "intellipaat": echo "company choosed: intellipaat"; 
break; 
default: echo "Company not found"; 
break;
 }
 ?>

Example- switch case in php- Edureka

With this, we come to an end of this article. I hope you have learned about switch case in PHP and the basic idea behind the switch case, that we have done something different inside the browser depending on some kind of scenario. 

Check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”PHP Tutorial” and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

All you Need to Know about Switch Case in PHP

edureka.co