PHP switch case explained with clear examples
See how PHP switch case works with clear examples. Compare multiple conditions and run the right code without stacking if statements.


In PHP, the “switch” construct lets you check multiple conditions and run different actions based on them. The “case” construct refers to the conditions used inside the “switch” construct. For example, you might want to perform different actions depending on the value of a number a user enters. In that case, you can use the “switch” construct.
<?php
// The number entered by the user
$sayi = 3;
// Switch construct
switch($sayi){
case 1:
echo "Girilen sayı 1'dir.";
break;
case 2:
echo "Girilen sayı 2'dir.";
break;
case 3:
echo "Girilen sayı 3'dür.";
break;
default:
echo "Girilen sayı 1, 2 veya 3 değildir.";
}
?>
In this code block, the variable holding the number the user entered is checked inside the “switch” construct. If the number is 1, the message for 1 is printed; if it is 2, the message for 2 is printed; and if it is 3, the message for 3 is printed. If the number the user entered is anything other than 1, 2, or 3, the default message is printed.
The “break” statement is used inside the “switch” construct and stops checking the remaining conditions once a matching condition is found. So after the action you want to run for the matching condition has been performed, the “break” statement stops checking the other conditions. The “default” statement, on the other hand, is used for cases where no matching condition is found.
As in this example, the “switch” construct makes it possible to check multiple conditions and perform different actions based on them. This construct can be more readable and easier to understand than an if-else construct. Especially when you need to check several conditions, using a “switch” construct may be the better fit.
Also, the “switch” construct isn’t limited to numeric or string values. For example, you might want to perform different actions depending on an object’s type. In that case, you can check the object’s type inside the “switch” construct.
<?php
class Araba {
public $marka;
}
$araba = new Araba();
$araba->marka = "Mercedes";
switch(get_class($araba)){
case "Araba":
echo "Bu bir araba nesnesidir.";
break;
case "Otomobil":
echo "Bu bir otomobil nesnesidir.";
break;
default:
echo "Bu nesnenin türü belirlenemiyor.";
}
?>
In this code block, the class of the car object that was created is retrieved with the “get_class()” function and checked inside the “switch” construct. If its class is “Araba”, the message for a car object is printed. If its class is “Otomobil”, the message for an automobile object is printed. For any other class, the message that the type cannot be determined is printed.
In summary, the “switch” construct in PHP lets you check multiple conditions and perform different actions based on them. The “case” construct refers to the conditions used inside the “switch” construct. Using this construct can be more readable and easier to understand than an if-else construct. It also isn’t limited to numeric or string values; you can check other kinds of values too, such as an object’s type.
Share it if it helped


