Aims:
elseif, we can have a conditional with
more than two arms:
if ( Boolean expression )
{
zero, one or more statements
}
elseif ( Boolean expression )
{
zero, one or more statements
}
else
{
zero, one or more statements
}
elseifs as you need
if ( $grade < 40 )
{
echo '<p>Fail</p>';
}
elseif ( 40 <= $grade && $grade < 45 )
{
echo '<p>Pass degree</p>';
}
else
{
echo '<p>Honours degree</p>';
}
| Grade | Outcome |
|---|---|
| less than 40% | Fail |
| 40% and above but less than 45% | Pass |
| 45% and above but less than 50% | Third class Hons |
| 50% and above but less than 60% | Second class Hons Grade 2 |
| 60% and above but less than 70% | Second class Hons Grade 1 |
| 70% and above | First class Hons |
| If your salary is over | But not over | Your whole salary is taxed at |
|---|---|---|
| 0 | 20000 | 15% |
| 20000 | 50000 | 28% |
| 50000 | 45% |
tax.html contains this form:
<form action="tax.php" method="get">
<input type="salary" name="salary" value="0" />
<input type="submit" />
</form>
tax.php
tax.php
$salary = (int) $_GET['salary'];
if ( $salary <= 20000 )
{
$tax = 0.15 * $salary;
}
elseif ( $salary <= 50000 )
{
$tax = 0.28 * $salary;
}
else
{
$tax = 0.45 * $salary;
}
echo "<p>Send us {$tax} eurines or your first-born child</p>";
if ( $salary <= 50000 )
{
$tax = 0.28 * $salary;
}
elseif ( $salary <= 20000 )
{
$tax = 0.15 * $salary;
}
else
{
$tax = 0.45 * $salary;
}
if ( 0 <= $salary && $salary <= 20000 )
{
$tax = 0.15 * $salary;
}
elseif ( 20000 < $salary && $salary <= 50000 )
{
$tax = 0.28 * $salary;
}
elseif ( 50000 < $salary )
{
$tax = 0.45 * $salary;
}
if ( 20000 < $salary && $salary <= 50000 )
{
$tax = 0.28 * $salary;
}
elseif ( 0 <= $salary && $salary <= 20000 )
{
$tax = 0.15 * $salary;
}
elseif ( 50000 < $salary )
{
$tax = 0.45 * $salary;
}
if ( 0 <= $salary && $salary <= 20000 )
{
$tax = 0.15 * $salary;
}
if ( 20000 < $salary && $salary <= 50000 )
{
$tax = 0.28 * $salary;
}
if ( 50000 < $salary )
{
$tax = 0.45 * $salary;
}