Transfer of Control In PHP

if-Else Statements in PHP

In IF structure if the condition is true then statements in the body of IF will be executed and if condition is false then nothing will be executed and control transfer to next statement. But if you want to execute some statement in case of False result then you will use the IF and ELSE structure. In this structure if condition is true then statements in body of IF will be executed other wise the statements in body of ELSE structure will be executed. This also done by using two separate IF Statement.
Syntax:
if (expersion)
{
statement 1;
statement 1;
statement 1;
}
else
{
statement 1;
statement 1;
statement 1;
}
For example if we check the user name and use is tauqeer then its welcome otherwise user ask to login first. Now first we use Two separate IF statement to do this:


<?php

$user_name='Tauqeer';


if ($user_name=='Tauqeer')
echo " you are welcome to learninghints.com";


if ($user_name!='Tauqeer')

echo "sory please log in first";

?>

Now do this with single IF and ELSE structure.


<?php

$user_name='Tauqeer';
 

if ($user_name=='Tauqeer')
 

echo " you are welcome to learninghints.com";
 

else
 

echo "sory please log in first";
 

?>