PHP 8 breaks on non-parenthesised ternary logic in plural functions

Asked by Doris Tam

Hi there,

Since switching to PHP 8 on my server, an error is thrown when I pick languages with more than two plural forms.

E.g. Slovenian: (n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0)

Error: Unparenthesized `a ? b : c ? d : e` is not supported.

Ref: https://translations.launchpad.net/mahara-lang/trunk/+pots/mahara/sl/+details

How do we update these functions?

Kind regards,
Doris

Question information

Language:
English Edit question
Status:
Answered
For:
Launchpad itself Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Quentin Debhi (ruinedyourlife) said :
#1

Hi Doris,

Iirc, in PHP 8, ternary operators require explicit parenthesization. So nested ternary operations without parentheses would be causing the error you're seeing.

Could you try something like this?
```
n%100==1 ? 1 : (n%100==2 ? 2 : ((n%100==3 || n%100==4) ? 3 : 0))
```

Here, each condition is wrapped in parentheses to clearly define the evaluation order.

Can you help with this problem?

Provide an answer of your own, or ask Doris Tam for more information if necessary.

To post a message you must log in.