if runs the first block whose condition is truthy; elseif may repeat and else catches the rest. else if works with braces, but only elseif works in the template colon syntax (if (...): ... endif;, PHP in HTML). Return early, and reject bad input first with a guard clause, so the main path is not buried in nesting.
<?php
function shipping(float $subtotal, string $country): float
{
if ($country === 'US') {
return $subtotal >= 50 ? 0.0 : 4.99;
} elseif ($country === 'CA' || $country === 'MX') {
return 9.99;
}
return 19.99; // every other country
}
echo shipping(60, 'US'), " ", shipping(20, 'US'), " ", shipping(20, 'MY'), "\n";
$truthy = array_filter([0, 0.0, "", "0", [], null, false, "0.0", " ", [0]]);
echo json_encode(array_values($truthy)), "\n"; // array_filter() keeps truthy valuesOutput
0 4.99 19.99 ["0.0"," ",[0]]
Seven values are false: 0, 0.0, "", "0", [], null and false; "0.0", a space and [0] are true. "0" is the trap: a form field holding 0 fails if ($value), so test $value !== '' instead.