Mastering the if-else Statement in PHP: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-18 09:14:01   5419  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

You can enhance the decision making process by providing an alternative choice through adding an else statement to the if statement. The if...else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false. It can be written, like this:

Syntax:



if(condition){
    // Code to be executed if condition is true
} else{
    // Code to be executed if condition is false
}


Example:



<?php
$overtime=60;
if ($overtime<=50)
{
$pay_amt=1200;
$medical=1000;
echo "Pay Amount : $pay_amt : Medical : $medical";
}
else
{
$pay_amt=2000;
$medical=1500;
echo "Pay Amount : $pay_amt : Medical : $medical";
}
?>


Output:

If you will rum the above code, you will get the following output.



Pay Amount : 2000 : Medical : 1500