CopyPastehas never been so tasty!

Switch statement in php

by anonymous

  • 0
  • 0
  • 0
77 views

http://codingvilla.com/switch-statement-in-php-single-article-670.aspx

 

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.

Add A Comment: