PHP HOME
PHP Introduction
PHP SETUP
PHP Syntax
PHP Data Types
PHP Outputs
PHP Variable
PHP Constant
PHP Strings
PHP Operators
PHP if else if
PHP Switch
PHP while loop
PHP for loop
PHP Arrays
PHP Functions
PHP Sorting Array
PHP Form Handling
PHP Form Validation
PHP Form Required
PHP Form URL/EMAIL
PHP Form Submit
PHP Contact Form
PHP DATE & TIME
PHP INCLUDE
PHP FILE HANDLING
PHP FILE OPEN/READ
PHP FILE CREATE/WRITE
PHP FILE UPLOAD
PHP SESSION
PHP COOKIES
PHP FILTER
PHP ERROR HANDLING
PHP EXCEPTION
PHP Redirects
PHP with MySQL
PHP WITH MYSQL DB
MYSQL CONNECT
MYSQL CREATE DATABASE
SELECT DATABASE
MYSQL CREATE TABLE
MYSQL INSERT DATA
MYSQL SELECT DATA
MYSQL UPDATE DATA
MYSQL DELETE DATA
MYSQL ORDER
MYSQL LIMIT
MYSQL LAST ID
You can execute one of many parts of code by using a switch statement in PHP. A switch statement is used for avoiding the long block of codes.
Why use Switch Statement?
Switch Statement can execute one of many parts of code by using in PHP. Switch statement is used for avoiding long block of codes.
switch (expression){
case first_label:
code to be executed if expression = label1;
break;
case second_label:
code to be executed if expression = label2;
break;
default:
code to be executed if the expression is different from both first_label and second_label;
}
Example of Switch statement
<!DOCTYPE html>
<html>
<body>
<?php
$variable = 'float';
switch ($variable){
case "integer":
$value = "This is integer type";
break;
case "string":
$value = "This is string type";
break;
case "float":
$value = "This is float type";
break;
default:
$value = "This is other type";
}
?>
<p>one statement will be correct : <?php echo $value;?></p>
</body>
</html>
The above code produces the following results
Outputs: one statement will be correct: This is float type