Why doesn't PHP throw a parsing error?

There are many "Why does PHP throw an error here?" questions. Well, that is a little different. I found the following code while processing code written by a colleague:

foreach($arr as $key => $value) {http://google.com/ echo $value; // ... } 

My first thought: β€œUmm ... how embarrassing, he must have accidentally pasted it there ...” and then: β€œWait ... there is no way this code really works ... it should be a syntax error " But still:

  $ php -l test.php No syntax errors detected 

And indeed (like many PHP codes that don't seem to be running), it works without problems. So I did a little testing:

 foreach($arr as $key => $value) {http://google.com/ <-- original, no error foreach($arr as $key => $value) {http: <-- also no syntax error foreach($arr as $key => $value) {http <-- bingo! "Unexpected T_ECHO..." 

What little tidbit of PHP grammar produces such weird results?

(I am using PHP 5.3.5)

+6
source share
1 answer

http: interpreted as a label (which is used for goto ), and //google.com/ is //google.com/ as a comment (which can be easily seen with syntax highlighting).

Goto documentation :

The goto operator can be used to jump to another section of the program. The target point is indicated by a label followed by a colon , and the instruction is indicated as goto, followed by the desired target label.

+7
source

Source: https://habr.com/ru/post/906440/


All Articles