If, Elseif and Else syntax
Similar to Switch syntax, maybe the most comprehensive for newbees. A bit slower but "safe"...
Published 15.08.2006 - Last edited 15.08.2006 - 6241 views - 0 comments
If syntax:
<?php
If (condition)
{
# some code
}
else
{
# other code
}
?>
If "condition" is true some code is run, if not (else) other code is run.
Example:
<?php
$condition1 = 1;
$condition2 = 0;
If ($condition1)
{
echo "<p>condition 1 is true</p>";
}
else
{
echo "<p>condition 1 is false</p>";
}
If ($condition2)
{
echo "<p>condition 2 is true</p>";
}
else
{
echo "<p>condition 2 is false</p>";
}
?>
The outcome of the examples are:
condition 1 is true (1 = true)
condition 2 is false (0 = false)
Elseif syntax:
<?php
If ($condition1)
{
# run code 1
}
elseif($condition2)
{
# run code 2
}
elseif($condition3)
{
# run code 3
}
else
{
# run code 4
}
?>
The difference compared to the If syntax is that you can check for more conditions (similar to Switch syntax).
Eksempel:
<?php
$condition1 = 0;
$condition2 = 1;
$condition3 = 2;
If ($condition1)
{
echo "<p>condition 1 er sann</p>";
}
elseif ($condition2)
{
echo "<p>condition 2 er sann</p>";
}
elseif ($condition3)
{
echo "<p>condition 3 er sann</p>";
}
else
{
echo "<p>Kunne ikke finne noen av conditionene</p>";
}
?>
Even if $condition3 also is true, the script jumps out of the Elseif at $condition2 as this is the first true condition.
Grabbing remote content
Up one level
PHP Sniplets