- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP switch Statement
The PHP switch statement is used when we need to execute particular block of code based on multiple cases/labels (conditions). For example:
<?php $x = 4; switch($x) { case 1: echo "The value of \$x is 1"; break; case 2: echo "The value of \$x is 2"; break; case 3: echo "The value of \$x is 3"; break; case 4: echo "The value of \$x is 4"; break; case 5: echo "The value of \$x is 5"; break; case 6: echo "The value of \$x is 6"; break; case 7: echo "The value of \$x is 7"; break; case 8: echo "The value of \$x is 8"; break; default: echo "The value of \$x is unknown"; } ?>
The output produced by above PHP example, is shown in the snapshot given below:
That is, the code inside the () bracket of switch considered as a single expression, that will be evaluated once. Therefore $x evaluated and the value after evaluation will be 4, as the value of $x is 4. Now this 4 gets compared with each and every case in the structure. Since 4 matches with case 4, therefore the block of code associated with this case, will be executed.
Note - The break keyword/statement is used, to skip the execution of remaining cases.
Note - The default case is used to execute some block of code, if no match found.
We can use switch statement to replace if...elseif...else. For example, the above example can also be written using if...elseif..else statement:
<?php $x = 4; if($x==1) echo "The value of \$x is 1"; elseif($x==2) echo "The value of \$x is 2"; elseif($x==3) echo "The value of \$x is 3"; elseif($x==4) echo "The value of \$x is 4"; elseif($x==5) echo "The value of \$x is 5"; elseif($x==6) echo "The value of \$x is 6"; elseif($x==7) echo "The value of \$x is 7"; elseif($x==8) echo "The value of \$x is 8"; else echo "The value of \$x is unknown"; ?>
You will get same output as the output produced by previous example.
PHP switch Statement Syntax
The syntax of switch statement in PHP, is:
switch(x) { case label1: // block of code to execute, if x==label1 break; case label2: // block of code to execute, if x==label2 break; case label3: // block of code to execute, if x==label3 break; . . . case labelN: // block of code to execute, if x==labelN break; default: // block of code to execute, if no match found }
PHP switch Statement Example
<?php $day = date("D"); switch ($day) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "What! are you an alien?"; } ?>
Since it is Sunday here, when I'm writing this post, therefore the output should be:
Today is Sunday
« Previous Tutorial Next Tutorial »
Follow/Like Us on Facebook
Subscribe Us on YouTube