PHP include() 返回值不一致还是我的无知?
哦,好吧,也许这是我的无知,但是关于 include
< 的 PHP 文档/a> 声明说比较返回值时要小心:
<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
echo 'OK';
}
// works
if ((include 'vars.php') == 'OK') {
echo 'OK';
}
?>
但这就是我所经历的; boolean false
如果失败,int 1
如果成功:
$exists = 'Found.php';
var_dump((include $exists)); // Type is: int, value is: 1
$notexists = 'NotFound.php';
var_dump((include $notexists)); // Type is: boolean, value is: false
这是我的错吗?为什么返回值不一致(例如,并不总是布尔值)并且与 PHP 文档不同?
Oh well, maybe it's my ignorance, but PHP documentation about include
statement says take care when comparing return value:
<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
echo 'OK';
}
// works
if ((include 'vars.php') == 'OK') {
echo 'OK';
}
?>
But this is what i've experienced; boolean false
in case of fail, int 1
if success:
$exists = 'Found.php';
var_dump((include $exists)); // Type is: int, value is: 1
$notexists = 'NotFound.php';
var_dump((include $notexists)); // Type is: boolean, value is: false
Is this my bad? Why returning value is inconsistent (meaning not always boolean
, for example) and differs from PHP documentation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这正是文档中所述的内容:
当它变得不一致时,就是当你实际返回包含文件中的某些内容时,
例如在 Found.php 中:
然后
还值得一读 PHP 中布尔值的计算方式:
http://php.net/manual/en/language.types.boolean.php
Well this is exactly what the documentation states:
When it becomes inconsistent is when you actually return something in the included file,
e.g. in Found.php:
And then
It is also worth reading up on how booleans evaluate in PHP:
http://php.net/manual/en/language.types.boolean.php