There are three types of conditional statements. They are as follows:
1) if statement :
This statement is used to execute some code only if a specified condition is true.
Syntax:
if(condition)
{
write statement here
}
2) if….else statement:
If a condition is true, one code is executed and another code if the condition is false.
Syntax:
if(condition)
{
write statement here
}
else if(condition)
{
write statement here
}
else
{
write statement here
}
3) switch statement:
This statement is used to select one of many blocks of code to be executed.
Syntax:
switch(variable)
{
case 1:
write statement to be executed
break;
case 2:
write statement to be executed
break;
case 3:
write statement to be executed
break;
default:
write statement to be executed
}
Examples
1) if statement
<?php
$day=”Fri”;
if($day==”Fri”)
{
echo “Have a nice weekend!”;
}
?>
2) if….else statement
<?php
$day=”Fri”;
if($day==”Fri”)
{
echo “Have a nice weekend”;
}
else
{
echo “Have a nice day”;
}
?>
3) switch statement
<?php
$x=0;
switch($x)
{
case 1:
echo “Number 1”;
break;
case 2:
echo “Number 2”;
break;
case 3:
echo “Number 3”;
break;
default:
echo “No number between 1 to 3”;
}
?>
- if statement
- if else statement
- switch statement
1) if statement :
This statement is used to execute some code only if a specified condition is true.
Syntax:
if(condition)
{
write statement here
}
2) if….else statement:
If a condition is true, one code is executed and another code if the condition is false.
Syntax:
if(condition)
{
write statement here
}
else if(condition)
{
write statement here
}
else
{
write statement here
}
3) switch statement:
This statement is used to select one of many blocks of code to be executed.
Syntax:
switch(variable)
{
case 1:
write statement to be executed
break;
case 2:
write statement to be executed
break;
case 3:
write statement to be executed
break;
default:
write statement to be executed
}
Examples
1) if statement
<?php
$day=”Fri”;
if($day==”Fri”)
{
echo “Have a nice weekend!”;
}
?>
2) if….else statement
<?php
$day=”Fri”;
if($day==”Fri”)
{
echo “Have a nice weekend”;
}
else
{
echo “Have a nice day”;
}
?>
3) switch statement
<?php
$x=0;
switch($x)
{
case 1:
echo “Number 1”;
break;
case 2:
echo “Number 2”;
break;
case 3:
echo “Number 3”;
break;
default:
echo “No number between 1 to 3”;
}
?>
Comments
Post a Comment