PHP Basic
Introduction to PHP PHP Starting Requirements Downloading XAMPP Local Server PHP Variable Concept and Data Type PHP Coding Syntax Showing Output in PHP PHP String Concatenation Increment & Decrement in PHP PHP Logical Operator and Usage Part-1 PHP Logical Operator and Usage Part-2 PHP Comparison Operators Arithmetic and Assignment Operators PHP Switch Case Statement For loop in PHP Super Global Variable $_REQUEST and $_GET Part-1 String Length in PHP Function Declaration and UsageSwitch Case statement is a special type of system in every programming language. Like other programming languages, php has also a system for using Switch Case statement. Switch Case mainly checks several conditions that is needed by a programmer. It every time takes a value. After that, it tries to match with the case. If it matches the case then next expression is done. Let's know how to declare Switch Case statement in PHP.
For writing Switch Case it necessary to use word switch. After that parenthesis () is required. After completing parenthesis, curly brace {} is needed. Inside curly brace different cases is written by case keyword. A user can check as many as conditions by using case. Every time colon (:) sign is added after the word case. Each case ends with a break. Because if break is not used after case then next cases will also be executed. It is so boring and not a good style of coding. default keyword is used for showing default value when no case is matched. Let's see an example below.
<?php
switch ($data) {
case 1:
//expression
break;
case 2:
//expression
break;
case 3:
//expression
break;
default:
//expression
break;
}
?>
We have used an example above where there is a process of writing Switch Case statement. We will now write a program to use a switch case.
<?php
$data = 2;
switch ($data) {
case 1:
echo "Executed at case 1";
break;
case 2:
echo "Executed at case 2";
break;
case 3:
echo "Executed at case 3";
break;
default:
echo "No Case matched";
break;
}
?>
As the $data is matched with case 2: , so case 2 will be executed. And finally it will give output like below
Executed at case 2
If we don't use break keyword after every expression. Then other cases will be executed also like below.
<?php
$data = 2;
switch ($data) {
case 1:
echo "Executed at case 1";
break;
case 2:
echo "Executed at case 2
";
case 3:
echo "Executed at case 3
";
default:
echo "No Case matched";
}
?>
Output:
Executed at case 2
Executed at case 3
No Case matched
default keyword is mainly used for showing a default value if no case is matched. let's see
<?php
$data = 10;
switch ($data) {
case 1:
echo "Executed at case 1";
break;
case 2:
echo "Executed at case 2
";
case 3:
echo "Executed at case 3
";
default:
echo "No Case matched";
}
?>
Here the value of $data is not matched with case. For this reason, default is executed and given output like below
No Case matched
A user can use unlimited cases for checking different types of value. Hope that you got a better idea in this Switch Case statement. Your practice will make you a great coder. do it...
If you think you have query... then For contacting with us use Facebook . You can also use Github