if / switch statement
if Statement

if statement is use to check condition if condition is true then body of your code will be  execute.
Syntax:-
if (condition)
{
body of your code;
}

Example 1:-

<?php
$x=25;
if($x>0)
echo "Number is postive";
?>

Output:-
Number is postive

if...else Statement

if statement is use check the condition, if condition is true then body of your code will be execute otherwise else part will be execute.

Syntax:-
if (condition)
{
body of your code;
}
else
{
body of your code;
}

Example 2:-

<?php
$x=25;
if($x>0)
echo "Number is Positive";
else
echo “Number is Negative”;
?>


output
Number is Positive

elseif Statement

if you have one more condition then use elseif statement.

Syntax:-
if (condition)
{
body of your code;
}
elseif (condition)
{
body of your code;
}
else
{
body of your code;
}

Example 4:

<?php
$x=2;
switch($x)
{
case 1:
echo "Your Choice is one ";
break;
case 2:
echo "Your Choice is two ";
break;
case 3:
echo "Your Choice is three ";
break;
default :
echo "Wrong Choice";
}
?>

Output:-
Your Choice is two

Example 3:

<?php
$x=25;
$y=45;
if($x>$y)
echo "First Number is Greater";
elseif($x<$y)
echo "Second Number is Greader";
else
echo "Both Number is equal";
?>

output
Second Number is Greater

Note:-

curly braces({ }) is not required when if and else block has single statement.

Switch Statement

Switch is basically use to make menu driven programme.In switch there many block but we select any one block depend upon user choice.

Syntax:
switch (variable)
{
case label1:
code;
break;
case label2:
code;
case label3:
code;
break;
default:
code to be executed if n is different from both label1 and label2;
}

Example 4:

<?php
$x=2;
switch($x)
{
case 1:
echo "Your Choice is one ";
break;
case 2:
echo "Your Choice is two ";
break;
case 3:
echo "Your Choice is three ";
break;
default :
echo "Wrong Choice";
}
?>

Output:-
Your Choice is two


Previous
Next