Example of nested ternary not acting the same in JavaScript and PHP.
Furthermore, PHP 8.0 now throws a fatal error when nested ternaries are used without parentheses.
Conclusion: just don't use nested ternaries.
# Run the JavaScript example
./ternary.js
# Run the Python example
./ternary.py
# Run the PHP examples using Docker
docker-compose up --build
JavaScript
two
Python
two
ternary-php7-1 | PHP 7.4.33
ternary-php7-1 | three
ternary-php7-1 exited with code 0
ternary-php8-1 |
ternary-php8-1 | Fatal error: Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /usr/src/myapp/ternary.php on line 9
ternary-php8-1 exited with code 255