PHP If Statements
Using if statement, PHP has the ability to make distinctions between
different possibilities. For example, you might have a script that checks if
a variable consists certain type of values. Here is syntax for
PHP if statement.
<?php
if ( condition is true then) { do something;
}
?>
Check the following if statement that checks if a variable
has a value equal to 1 and also uses else option of if statement.
If the condition is true, it writes first statement, if condition is not true, it writes the second statement.
<?php
$n=1;
if ($n==1)
{
echo("N has the value equal to 1");
}
else
{
echo("N is not equal to one");
}
?>
|
Nested If Statement
You would be able to check variety of conditions based on deversified values.
The following example is nested if statement to check
two conditions:
<?php
$fruit1="Apple";
$fruit2="Orange";
$in_store=1;
if($in_store==1)
{
print "$fruit1 is in store";
}
elseif($in_store==2)
{
print"$fruit2 is in store";
}
else
{
print"No value specified";
}
?>
| This if statement will check if the variable in_store
is equal either 1 or 2 and displays fruit1 or fruit2 according to the
true condition. print will display the value
of the specified variable. It's similar to echo.
Execution result: Apple is in store
|
|