- Normally in PHP programming language statement are execute in a sequenced manner.
- Sometime we need to execute some statement based on some specific condition.
- Depending on the condition either we have to execute or skip some statement.
- In such situation we need to take decision to transfer control flow on the required statement.
- PHP Programming language provides the following decision making statement:
(1) If Statement
-
If Statement:
- When we want to execute some statement based on a given condition that time we can use if statement.
- If statement is a two way decision making statement.
- It provides two alternative paths to be followed according to the condition.
- The following are different from of if statement.
(1) Simple if Statement:
- It is a simple form of if statement in which some statement are either executed or skipped according to the given condition.
- It has the following form:
- It first checks the condition or expression given inside if.
- If condition becomes true, control enters into if block and statements inside the block are executes.
- If condition becomes false then statements inside if block are skipped and control transfers to the statement following by if statement.
- When if statement has a single statement then braces are not required but it is necessary when if statement contain two or more statement
if (condition)
{
Statement(s);
}
Example:
<?php
if (category =="sport")
{
mark = mark + bonus_mark;
}
?>
- It first checks the condition or expression given inside if.
- If condition becomes true, control enters into if block and statements inside the block are executes.
- If condition becomes false then statements inside if block are skipped and control transfers to the statement following by if statement.
- When if statement has a single statement then braces are not required but it is necessary when if statement contain two or more statement
- The following program demonstrates the use of if…else statement to find out maximum number.
<?php
// program to find out maximum number
$x = 10, $y = 20 ;
if ($x > $y)
echo "X is maximum" ;
else
echo "Y is maximum" ;
?>