Pages

Saturday 16 June 2012

SWITCH Statement

We use the switch statement to select one of many blocks of code to be executed.The syntax for switch statement is very much similar to that of C or C++.
Syntax:-
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
Explanation:-
The switch(n) is starting of the statement.At next line { indicates starting of body of switch(n).Please note before starting the switch statement we have to declare variable whatever we take like we have taken here n in the syntax.we use dollar sign ($) then name of variable like:- $n.

Then different cases or multiple choices are filled according to the requirement.To we use break statement to prevent the code from running into the next case automatically. The default statement is used if no match is found.
Now we take a simple example to explain above syntax. 
<?php  //PHP script starts
$d=date("D"); //variable declaration
switch ($d)     //switch statement
{                    //starting of switch
case "Mon":   //multiple statement begins
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 "Wonder which day is this ?";
}          //switch ends
?>        //PHP script ends




if ,else and statements in PHP

The If Statement:-
Syntax:-------------------------------

if (condition) code to be executed if condition is true;
for example:-

<?php
$d=date("D");
 if ($d=="Fri") echo "Have a very very nice weekend!";
?>


The output of above code will be -  Have a very very nice weekend!
This result will printed only if day is friday not on any other day.
So as to display a different output we use the else statement along with
the if statement. 
Here is the syntax for if-else statement.


if (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}



so, we can write code as-->



<?php
$d=date("D");
if ($d=="Fri")
{
echo "Have a nice weekend!";
}
else
{
echo "Have a nice day!";
}
?>

The output if the day is friday will be-
Have a very very nice weekend! 

The output if the day is not friday will be-
Have a nice day!



Note:-we have already discussed simple if-else

but nested if-else facility is also available in PHP

syntax==>for nested if-else-

if (condition)
{
code to be executed if condition is true;
}
elseif (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}

Possible example may be:-
<?php
$d=date("D");
if ($d=="Fri")
{
 echo "Have a nice weekend!";  //if day is friday this(<--) will be printed
}
elseif ($d=="Sun")
{
  echo "Have a nice Sunday!";   //if day is sunday
}
else
{
  echo "Have a nice day!";    //any other day except sunday and friady
}
?>

***************************