3
Consider the following code:
$a = $b > $c ? $c : $b;
The question is, if $b is a set of operations, if the logic result $b > $c is false, I will have to repeat the entire existing operation in $b.
I did it this way, but I wonder if there’s another way:
$a = $b;
$a = $a > $c ? $c : $a;
this is an "abuse" of the ternary operator since its code is less efficient than a
if()
. It makes no sense to make the logical comparison and an assignment that is not necessary.if($a > $c) $a=$c;
(assigns nothing inelse
(for not having the same)).– Mansueli
@Kyllopardiun Else would be necessary in the first example but not in the second. The question is the first example, if $b is a set of operations, it is not legal to rewrite everything if the result of logic is false, that is the question.
– Filipe Moraes
Philip, I cannot understand your question. It seems to me something obvious: if I want to keep a logical result to some extent what I must do? Store and that’s exactly what you did.
– Mansueli