Coding Villa

Switch statement in php

Author : nida khawar

Share with Friends
Views : 468
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this simple article you will learn that how we can use switch statement in php.


Advertisements


 

 

Switch statement:

 

Switch statement is a type of a selection control statement that exists in most of the programming languages. When we have two options we use if else statement but when we have more than two option we use switch option although that can also be handled by if else.

 

Switch statement in PHP:

 

In php switch statement can also be used for the same purpose. It allows control to transfer in different statement depending on the value that you have given. Value may be an integer or an enumeration. The syntax of the switch statement is

 

Syntax:

 

Switch (value)

              {

                       Case”1”:

                       Statement;

                       Break:

Case”2”:

                       Statement;

                       Break:

                    --------------------

Case”n”:

                       Statement;

                       Break:

          }

 

  

In this code we have one choice variable intialized with ROCK value and we have different case statement code to check the given value and run that statement and displays result. 

 

 

Code:

 

 

<?php

$choice = "Rock";

  switch ($choice){

     case "Pop":

         print "Pop not Rock";

         break;

     case "Rap":

         print "Rap and not Rock";

         break;

     case "Jazz":

         print "Jazz not Rock";

         break;

     case "Rock":

         print "Rock music";

         break;

?>

 

 

Above is a simple code to use switch statement in php.

 

This simple article tells that how we can use switch statement in php.

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook