You already start with a wrong assumption that these 2 code lines are identical, because they are not. The ternary (expression ? IF TRUE : IF FALSE
) operator is left associative.
So if you go through step by step you maybe see it better:
First ternary line:
echo 0 ?: 1 ?: 2 ?: 3; //1
With parentheses:
echo ((0 ?: 1) ?: 2) ?: 3; //1
└──────┘ //0 → FALSE
↓ //Second expression: 1
echo ((1) ?: 2) ?: 3; //1
└────────┘ //1 → TRUE
↓ //First expression: 1
echo (1) ?: 3; //1
└──────┘ //1 → TRUE
↓ //First expression: 1
echo 1; //1
Second ternary line:
echo 0 ? 0 : 1 ? 1 : 2 ? 2 : 3; //2
With parentheses:
echo ((0 ? 0 : 1) ? 1 : 2) ? 2 : 3; //2
└─────────┘ //0 → FALSE
↓ //Second expression: 1
echo ((1) ? 1 : 2) ? 2 : 3; //2
└───────────┘ //1 → TRUE
↓ //First expression: 1
echo (1) ? 2 : 3; //2
└─────────┘ //1 → TRUE
↓ //First expression: 2
echo 2; //2