Why are all single words in php lines

So, I ran into this feature / bug today, and I'm struggling to wrap my head around what is happening.

so we all know that in php this is:

echo 'hello'; print 'world'; echo gettype('test'); 

will return this:

 hello world string 

However, until today I did not know what it is:

 echo hello; print world; echo gettype(hello); 

will return this:

 hello world string 

What's going on here? What happens during the php compilation process that it sees any one word as a string? Does it have a name? Does it have any utility?

+4
source share
2 answers

If you do not wrap a piece of "string" inside quotation marks (whether double or single), the interpreter will try to make a guess and apply it to the string. I believe that when you run this PHP fragment in error_reporting (E_ALL) you will see a notification

 Notice: Use of undefined constant string - assumed 'string' in Command line code on line 1 

Good practice is to always include error_reporting in E_ALL in the development settings, but do not forget to turn it into less descriptive during the production process, you do not want people to have access to confidential information (such as the path) if your php errors of.

+4
source

If PHP cannot find a function or constant named hello , then yes, it treats it as a string. However, this is bad practice, and it is even warned:

 >php -r 'error_reporting(E_ALL|E_STRICT);echo gettype(blah);' PHP Notice: Use of undefined constant blah - assumed 'blah' in Command line code on line 1 

So, the moral of the story is to always enable error_reporting in PHP (just like you should always use strict and use warnings in Perl).

+7
source

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


All Articles