TL;DR
PHP was being run via a batch file which was mangling the escaping.
Partial answer:
It's the first closing parenthesis inside the quoted string that causes the error. Breaking any number of consecutive parentheses out of the string fixes the error:
C:\>php -r "echo '"""' . trim(trim(trim(trim(42"))))" . '"""' . trim(trim(trim(13)));"
"42"13
Note the four consecutive parentheses after 42
that are NOT in the quoted string (so there can be no spaces or a different error occurs). Once another double quote is found, it's no longer necessary to break the closing parentheses out, e.g. the second set of nested trim()
functions. Adding a second double quote at the start needs the parentheses treated the opposite way:
C:\>php -r "echo '""""""' . trim(trim(trim(trim(42)))) . '"""' . trim(trim(trim(13")))";"
""42"13
This only works from PHP version 7.1 to 7.3(+?). PHP 7.0 and earlier give an error.
From PHP 5.4 to 7.0, three double quotes don't work, you have to use only two:
C:\>php70 -r "echo ' "" ' . trim(42) . ' "" ';"
" 42 "
C:\>php54 -r "echo ' "" ' . trim(42) . ' "" ';"
" 42 "
C:\>php70 -r "echo ' "" ' . trim(42) . ' "" ';"
" 42 "
C:\>php71 -r "echo ' """ ' . trim(42")" . ' """ ';"
" 42 "
C:\>php73 -r "echo ' """ ' . trim(42")" . ' """ ';"
" 42 "
The weird behaviour with the parentheses is caused by running PHP via a batch script. If run directly by calling the executable, the behaviour is less weird, although different versions of PHP need different levels of double quote escaping:
PHP | No. | "
---------------
5.0 | 3 | """
5.1 | 3 | """
5.2 | 3 | """
5.3 | 2 | ""
5.4 | 2 | ""
5.5 | 2 | ""
5.6 | 2 | ""
7.0 | 2 | ""
7.1 | 3 | """
7.2 | 3 | """
7.3 | 3 | """
I have now fixed the batch file. Details here: Fix a batch script to handle closing parentheses inside an if block