else in php

An else statement allows you to define an alternative action to be taken when the condition in an if statement is false

Example-1

if ($totalqty == 0) {
 echo "You did not order anything on the previous page!<br />";
} else {
 echo htmlspecialchars($tireqty).' tires<br />';
 echo htmlspecialchars($oilqty).' bottles of oil<br />';
 echo htmlspecialchars($sparkqty).' spark plugs<br />';
 }

Example-2

if ($totalqty == 0) {
 echo "You did not order anything on the previous page!<br />";
} else {
 if ($tireqty > 0)
echo htmlspecialchars($tireqty).' tires<br />';
 if ($oilqty > 0)
echo htmlspecialchars($oilqty).' bottles of oil<br />';
 if ($sparkqty > 0)
echo htmlspecialchars($sparkqty).' spark plugs<br />';
}

Leave a Comment