Is there a way to have an inline if statement in PHP which also includes a elseif?
I would assume the logic would go something like this:
$unparsedCalculation = ($calculation > 0) ? "<span style=\"color: #9FE076;\">".$calculation : ($calculation < 0) ? "<span style=\"color: #FF736A;\">".$calculation : $calculation;
In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.
The ELSEIF operation is the combination of an ELSE operation and an IF operation. It avoids the need for an additional level of nesting. The IF operation code allows a series of operation codes to be processed if a condition is met. Its function is similar to that of the IFxx operation code.
There is no difference between “elseif” or “else if” just another way to write it. If statements allow multiple elseifs which can then be followed by else. I prefer using 'else if', but that's just by convention and it tends to make the code look a little neater, for me.
The endif keyword is used to mark the end of an if conditional which was started with the if(...): syntax. It also applies to any variation of the if conditional, such as if... elseif and if...else .
elseif
is nothing more than else if
, so, practically, there is no elseif
, it's just a convenience. The same convenience is not provided for the ternary operator, because the ternary operator is meant to be used for very short logic.
if ($a) { ... } elseif ($b) { ... } else { ... }
is identical to
if ($a) { ... } else { if ($b) { ... } else { ... } }
Therefore, the ternary equivalent is
$a ? ( ... ) : ( $b ? ( ... ) : ( ... ) )
you can use nested Ternary Operator
(IF ? THEN : ELSE)
(IF ? THEN : ELSE(IF ? THEN : ELSE(IF ? THEN : ELSE))
for better readability coding standard can be found here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With