How to use Decision Making Statement in PHP
In this article, I will explain how the if, if..else statement can be used in PHP.
Decision Making Statement in PHP
PHP supports following decision making statements:
- if...statement -if statement is used, if you want to execute a set of code when a condition is true and condition is false.
- if...else statement -if...else is used to execute some code if a condition is true and another code if the condition is not true (false).
- if...elseif....else statement - This statement is used to select one of several blocks of code to be executed.
The if Statement
you want to execute some code only if a specified condition is true.
Syntax
if (condition)
This code to be executed if condition is true;
|
Example
The following example show greater value between given values.
<html>
<body>
<h3 style="color: brown;">If statement example</h3>
<?php
$a=50;
$b=20;
if ($a>$b)
echo "The value of x1 greater than y1";
?>
</body>
</html>
|
Output
The if...else Statement
if....else statement to execute some code if a condition is true and another code if a condition is false.
Syntax
if (condition)
This code to be executed if condition is true;
else
This code to be executed if condition is false;
|
Example
The following example show how the if...else statement can be used in PHP.
<html>
<body>
<h3 style="color: chocolate;">If-else statement example</h3>
<?php
$worktime=60;
if ($worktime<=50)
{
$pay_amt=100;
$medical=50;
echo "Pay Amount : $pay_amt <br/> Medical Amount : $medical ";
}
else
{
$pay_amt=200;
$medical=100;
echo "Pay Amount : $pay_amt <br/> Medical Amount : $medical";
}
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here