Home Blog if Statement in PhP

if Statement in PhP

0
if Statement in PhP

The if Statement is the simplest conditional statements.

In if  Statements Output will appear when only Condition must be true.

If Statement works  much like the English language statement, “if X happens, then do Y.”

Syntax

if (condition) {
    code to be executed if condition is true;
}

In the example below will output “Have a good day!” if the current time (HOUR) is less than 20:

<?php
$t = date("H");

if ($t < "20") {
    echo "Have a good day!";
}
?> 

Example to find the sum of even number between 1 to 100

<?php
$sum=0;
 
for($i=1;$i<=100;$i++)
{
if($i%2==0)
{
$sum=$sum+$i;
}
}
echo $sum;
?>

Output: 2550

LEAVE A REPLY

Please enter your comment!
Please enter your name here