所以我今天遇到了这个功能/错误,我正在努力了解正在发生的事情。
所以我们都知道在 php 中是这样的:
echo 'hello';
print 'world';
echo gettype('test');
将返回这个:
hello
world
string
然而,直到今天我才意识到这一点:
echo hello;
print world;
echo gettype(hello);
将返回这个:
hello
world
string
这是怎么回事? php 的编译过程中发生了什么,它将任何单个单词视为字符串?这个有名字吗它有什么用处吗?
最佳答案
如果 PHP 找不到名为 hello
的函数或常量,那么可以,它会将其视为字符串。然而,这是不好的做法,甚至被警告不要这样做:
>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
因此,故事的寓意是在 PHP 中始终打开error_reporting
(就像您应该始终 使用严格
和 在 Perl 中使用警告
)。
关于php - 为什么php字符串中都是单字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9816278/