PHP -Building Blocks

Home / php-training-in-gurgaon / PHP -Building Blocks

If –else statements:

if…else statement – executes set of codes if a condition is true and another code if the condition is false

Within a statement, if a certain condition is true, then something happens. Otherwise, something else happens. We can continue adding else statements as many times as we need.

So, within PHP statement, we first define the variable and then set up the conditional statements like this:

Syntax:

if (condition)

code to be executed if condition is true;

elseif (condition)

code to be executed if condition is true;

else

code to be executed if condition is false;

Example 1:

<?php

$avg = 50

if ($avg>60)

{

echo “You have a good score”;

}

else

{

echo “Score is less than average”;

}

?>

Example 2: 

<?php

$status = 1;

if ($status== 1)

{

print (“<img src =images/right.jpg>”);

}

else

{

print (“<img src =images/cross.jpg>”);

}

?>

The variable called $status has been assigned a value of 1.The first line of if statement tests to see what is inside of the variable called $status. It‘s testing to see whether this variable has a value of 1.