【发布时间】:2015-10-17 13:08:15
【问题描述】:
我正在使用 SensioLabs Insight 将我的项目代码质量保持在所用工具的最佳实践之上。
此行在 SLInsight 分析期间导致警告:
$handle = fopen($file, 'w') or die('Cannot open file: '.$file);
SensioLabs 说:
应避免使用逻辑运算符。
[...]
or 运算符的优先级与 || 不同。这可能会导致意外行为,请使用 ||
好的,但是,如果我只是使用 ||而不是 'or' ,像这样:
$handle = fopen($file, 'w') || die('Cannot open file: '.$file);
由于fopen 失败,我得到了经典的No such file or directory 错误,
而不是我所期望的(死亡动作和返回消息)。
为避免这种情况,我在执行fopen 之前使用了一个条件:
if(!file_exists($file)) {
throw $this->createNotFoundException('Le fichier '.$file.' n\'existe pas.');
}
$handle = fopen($file'.log', 'r');
'||'有什么用在我想要的变量赋值中?
提前感谢您赐教。
【问题讨论】:
标签: php logical-operators