Because what you’ve written is the same as:
echo (true ? 1 : true) ? 2 : 3;
and as you know 1 is evaluated to true
.
What you expect is:
echo (true) ? 1 : (true ? 2 : 3);
So always use braces to avoid such confusions.
As was already written, ternary expressions are left associative in PHP. This means that at first will be executed the first one from the left, then the second and so on.
Related Contents:
- Stacking Multiple Ternary Operators in PHP
- How to write a PHP ternary operator [duplicate]
- Ternary operator and string concatenation quirk?
- What is ?: in PHP 5.3? [duplicate]
- Ternary operator left associativity [duplicate]
- Understanding nested PHP ternary operator [duplicate]
- What does ? … : … do? [duplicate]
- How to concatenate multiple ternary operator in PHP? [duplicate]
- C#’s null coalescing operator (??) in PHP
- Which coding style you use for ternary operator? [closed]
- php – Should I call exit() after calling Location: header?
- PHP ternary operator vs null coalescing operator
- Doctrine2: Best way to handle many-to-many with extra columns in reference table
- How to Create Multiple Where Clause Query Using Laravel Eloquent?
- What is the best IDE for PHP? [closed]
- Get the client IP address using PHP [duplicate]
- Why is mysqli giving a “Commands out of sync” error?
- PHPDoc type hinting for array of objects?
- How to get directory size in PHP
- What is the “->” PHP operator called? [closed]
- Reading an Excel file in PHP [closed]
- Php mail: how to send html?
- Ignore html tags in preg_replace
- Best way to manage long-running php script?
- Convert number to month name in PHP
- How to get last key in an array?
- Strange behavior of foreach when using reference: foreach ($a as &$v) { … }
- A form doesn’t submit [duplicate]
- Creating and Update Laravel Eloquent
- Laravel app stopped working after upgrading to php 8
- How do you make a string in PHP with a backslash in it? [closed]
- php sentence boundaries detection [duplicate]
- Serve image with PHP script vs direct loading an image
- Getting a modified preorder tree traversal model (nested set) into a
- Back button re-submit form data ($_POST)
- PHP String to Float
- Laravel Route issues with Route order in web.php
- Tracking Memory Usage in PHP
- Continue processing after closing connection [duplicate]
- What causes an HTTP 405 “invalid method (HTTP verb)” error when POSTing a form to PHP on IIS?
- PHP : binary image data, checking the image type
- Mysqli prepared statements build INSERT query dynamically from array
- make switch use === comparison not == comparison In PHP
- How can I paginate a merged collection in Laravel 5?
- Aggregated query without GROUP BY
- How to alias the name of a column in Eloquent
- How is testing the registry pattern or singleton hard in PHP?
- Having two different sessions in same domain
- How can I access an object property named as a variable in php?
- Warning: “continue” targeting switch is equivalent to “break”. Did you mean to use “continue 2”?