Search code examples
phpternary

Ternary operator vs. Logical


Could under any circumstances those two produce different results, or in general, is there any difference (performance, etc...) between them:

$r = this() ?: that();

and...

$r = this() or that(); // $r = this() || that();

Assuming there's no difference, which one would you suggest using and why?


Solution

  • The second one has a boolean result. There's no restriction on the first one.

    If you need a boolean value, use the logical operators.

    As code clarify comes first, and this is micro-optimisation territory, just ignore the performance difference (if there's any).