PHP IF ELSE statement script and example code |
PHP IF statement is popular and is used to check some conditions and
accordingly statements are executed. We can control the html display part also
by the IF STATEMENT. Here is one simple example of if statement.
$j = 5; $k = 10;
if ($j < $k){ print "Variable $j value is less than $k";
}
We can add ELSE condition to the above statement and that part will be executed
once the if condition does not satisfy.
$j = 10; $k = 5;
if ($j < $k){ print "Variable $j value is less than $k";
// This line will not be executed
}
else{
print "Variable $j value is greater than $k";
// This line will be executed
}
If we are checking the different values of a variable which can have more than
two conditions then for each case using PHP IF STATEMENT for matching
conditions is not a good idea. In such a case PHP
SWITCH STATEMENT is to be used.
One of the important use of PHP IF STATEMENT is when inside a php script we want to display
LOG IN or LOG OUT links.
|