1

From this answer to a previous question I've seen that to get just one double quote inside a double quoted string from the command line (not from PowerShell, if people are thinking that), it's necessary to use three double quotes in a row. So, I've been using three double quotes without issues until recently.

C:\>php -r "echo '"""' . trim(trim(trim(trim(trim(42))))) . '"""';"
)))) was unexpected at this time.

C:\>php -r "echo '"""' . trim(trim(trim(trim(42)))) . '"""';"
))) was unexpected at this time.

C:\>php -r "echo '"""' . trim(trim(trim(42))) . '"""';"
)) was unexpected at this time.

C:\>php -r "echo '"""' . trim(trim(42)) . '"""';"
) was unexpected at this time.

C:\>php -r "echo '"""' . trim(42) . '"""';"
. was unexpected at this time.

C:\>php -r "echo '"""' . '"""';"
""

Why don't the three double quotes work in the above examples except the last one?

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
  • you need like this:- `"echo '" . trim(trim(trim(trim(trim(42))))) ."';";` – Alive to die - Anant Nov 27 '19 at 05:43
  • `php -r "echo '"""' . trim(42) . '"""';";` add another semicolon at the end of line, this worked for me – Yash Karanke Nov 27 '19 at 05:43
  • @AnantSingh---AlivetoDie That gives me the same error. – CJ Dennis Nov 27 '19 at 05:48
  • 1
    @YashKaranke That gives me the same error. – CJ Dennis Nov 27 '19 at 05:48
  • @CJDennis Maybe it is related to the stack which you're using and PHP version, I am using XAMPP stack with PHP 7+. – Yash Karanke Nov 27 '19 at 06:22
  • 1
    Why not just escape the double quote in a correct way (`php -r "echo '\"' . trim(42) . '\"';" //output: "42"`)? This makes the code btw. also much better readable! – CodyKL Nov 27 '19 at 06:36
  • 1
    @YashKaranke The web server doesn't matter because it's CLI, not CGI. I'm using PHP 7.3.9, and oddly enough, PHP versions 7.1-7.3 behave the same, but PHP 7.0 is different! – CJ Dennis Nov 27 '19 at 06:38
  • @CodyKL That gives me the same error. – CJ Dennis Nov 27 '19 at 06:39
  • If this `php -r "echo '\"' . trim(42) . '\"';"` gives you the same error, than something is really wrong with your environment! This is just a simple output on the interactive shell which wrap the result from `trim` with double-quotes. – CodyKL Nov 27 '19 at 06:46

1 Answers1

0

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

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
  • And what for an important reason do you have to support **outdated/unsupported** PHP versions? There isn't official support for PHP <=7.0 anymore! – CodyKL Nov 27 '19 at 07:04
  • @CodyKL I'm using PHP 7.3, but I was curious how far back the behaviour goes. Is it something recent or has it been the same for the last 10 years or more? Experimenting shows it's recent, i.e. it looks like PHP broke something. – CJ Dennis Nov 27 '19 at 07:05