Thursday, December 1, 2011

Conditions In PHP

Conditions in php
If  Else
If else statements are used to check a condition is true or false and execute a group of statements if true other wiseexecute some other statements.The if statement also sub divided to Else if .... else.It is used to check more than one conditions in the same  set of codes.
if (codition)
{ set of codes to execute when condition is true}
else
{set of codes to execute when condition is false }


if (codition_one)
{ set of codes to execute when condition is true}
elseif
{set of codes to execute when condition_one is false and condition_two is true}
else
{set of codes to execute when conditions are false}

Eg:
<?php
$a=1;
if($a==1)
{
echo "number = 1";
}
elseif($a==0)
{ echo "Number is not zero";


}
else
echo "number is not 0 nor 1";
?>

Switch
Simply a Switch statement  is used to execute a group of codes when anyone of condition matches.
switch (some_variable)
case 1:
break;
case 2:
break; 
.
.
.
.
default :

Eg:

<?PHP
$week=2;
switch($week)
{
case 1:
echo "day is Sunday";
break;
case 2 :
echo "day is Monday";
break;
case 3:
echo "day is Tuesday ";
break;
case 4:
echo "day is Wednesday";
break;
case 5:
echo "day is Thursday ";
break;
case 6 :
echo "day is Friday";
break;
case 7:
echo "day is Saturday ";
break;
default :
echo "Invalid choise";
}

 Output is day is Monday




No comments:

Post a Comment