php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80528 No waring/error for some nested ternary operators without explicit parentheses
Submitted: 2020-12-17 16:17 UTC Modified: 2020-12-17 17:50 UTC
From: andrew dot engdahl at gmail dot com Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: andrew dot engdahl at gmail dot com
New email:
PHP Version: OS:

 

 [2020-12-17 16:17 UTC] andrew dot engdahl at gmail dot com
Description:
------------
As of 7.4 nested ternary operators without explicit parentheses are supposed to be deprecated. 
However, this is only true with the following syntax:

$test = $cond1 ? 1 : $cond2 ? 2 : 3; // Raises warning
$test = $cond1 ? 1 : ( $cond2 ? 2 : 3 ); // Documented correct syntax https://www.php.net/manual/en/migration74.deprecated.php

If the secondary condition is nested within the first condition's true expression no warning or error is raised


$test = $cond1 ? $cond2 ? 1 : 2 : 3; // No warning or error.

$cond1 = true & $cond2 = true returns 1
$cond1 = true & $cond2 = false returns 2
$cond1 = false returns 3


Test script:
---------------
$test = true ? true ? 1 : 2 : 3;
echo $test;

Expected result:
----------------
For 7.4
Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /in/V9r8u on line 1

For 7.8
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 /in/V9r8u on line 1

Actual result:
--------------
No warning or error

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-17 16:21 UTC] andrew dot engdahl at gmail dot com
Sorry in expected results the second version should be 8.0 instead of 7.8
 [2020-12-17 16:22 UTC] nikic@php.net
-Type: Bug +Type: Documentation Problem
 [2020-12-17 16:22 UTC] nikic@php.net
Nesting into the middle operand is not affected, because it is unambiguous. See https://wiki.php.net/rfc/ternary_associativity for precise rules.

Possibly the note in the migration guide needs to be more explicit?
 [2020-12-17 17:50 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2020-12-17 18:00 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=352118
Log: Fix #80528: No waring/error for some nested ternary operators without explicit parentheses
 [2020-12-17 18:02 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=4642b715f374b4220884fa110b6529ac948799b5
Log: Fix #80528: No waring/error for some nested ternary operators without explicit parentheses
 [2020-12-17 18:02 UTC] phpdocbot@php.net
-Status: Verified +Status: Closed
 [2020-12-18 09:59 UTC] mumumu@php.net
Automatic comment from SVN on behalf of mumumu
Revision: http://svn.php.net/viewvc/?view=revision&revision=352121
Log: Fix #80528: No waring/error for some nested ternary operators without explicit parentheses
 [2020-12-18 10:00 UTC] phpdocbot@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=4fbdbc912243c8f41a816a9e19530e4a527f34b3
Log: Fix #80528: No waring/error for some nested ternary operators without explicit parentheses
 [2020-12-30 11:58 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=ce42abe552f10329421f62fbd99752610392a335
Log: Fix #80528: No waring/error for some nested ternary operators without explicit parentheses
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 13:01:28 2025 UTC