Won't fix Unexpected ternary vs && operation behavior between templates and php

Xon

Well-known member
Affected version
2.0.12
Unexpected ternary vs && operation behavior between templates and php

This returns 3 in php;
PHP:
false && true ? 2 : 3;
true && false ? 2 : 3;

This returns false in templates;
PHP:
{{ dump(false && true ? 2 : 3) }}

This returns true in templates;
PHP:
{{ dump(true && false ? 2 : 3) }}
 
This is actually something we've been aware of from the beginning, but it's not something we're planning on making any changes to.

Although the syntax is PHP-like, it is actually parsed using a custom parser and so there may be subtle differences in how certain conditions are handled. Changes here would be a BC break as well.

All things considered, we're still not leaning towards any changes here, but there is an acceptable workaround which we do tend to employ in a few places where certain ternary conditions are used:
Code:
{{ dump((true && false) ? 2 : 3) }}
As well as being more clear to read IMO (more significant with slightly more complex conditions of course 😉), this would return the expected value of 3.
 

Similar threads

Back
Top Bottom