PHP Programing language

adplus-dvertising
PHP If-else
Previous Home Next

The if/ else statement is using in PHP for if any some body told you, "2+2=4 is true"? and another say "not" , Well you fail! This is an example of an if/else conditional statement.

2+2=4 is true Else, not, fail.

Someone comes to your website and you want to ask this visitor her name if it is her first time coming to your site. With an if statement this is easy. Simply have a conditional statement to check, "are you visiting for the first time". If the condition is true, then take them to the "Insert Your Name" page, else let her view the website as normal because you have already asked her for her name in the past.

Example:

<?php
$number_five = 5;
if ( $number_five == 5 ) 
	{
echo "The if statement evaluated to true ";
    } 
else 
	{
echo "The if statement evaluated to false";
    }
?>

Output

The if statement evaluated to true.

Execute Else Code with False:

The if the if statement was false, then the code contained in the else segment would have been executed. Note that the code within the if and else cannot both be executed, as the if statement cannot evaluate to both true and false at one time! Here is what would happen if we changed to $number_three to anything besides the number 5.

<?php
$number_five = 621;
if ( $number_five == 5 ) 
	{
echo "The if statement evaluated to true";
    } 
else 
	{
echo "The if statement evaluated to false";
    }
?>

Output

The if statement evaluated to false

The variable was set to 621, which is not equal to  5 and the if statement was false. As you can see, the code segment contained within the else was used in this case.

Previous Home Next