【问题标题】:Which is the better syntax while including a file in PHP? [duplicate]在 PHP 中包含文件时,哪种语法更好? [复制]
【发布时间】:2013-08-28 09:58:32
【问题描述】:

有些人包含这样的文件:

include ('file.php');

有些人包含这样的文件:

include 'file.php';

我认为这里完全没有必要使用括号。你有什么看法?哪种风格更好,为什么?

【问题讨论】:

标签: php syntax coding-style


【解决方案1】:

当不需要括号时,最好将其删除(returnreturn () 相同,性能更好)。包含可能相同。

http://php.net/manual/en/function.return.php

Note: Note that since return is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.

此外,使用完整路径进行包含比仅使用相对路径更好。

【讨论】:

  • 您能否提供引用以提高性能
  • 添加了来自 PhP 手册的引用。
【解决方案2】:

有点无关紧要,因为解析器同时接受两者(因此我怀疑你很可能有更紧迫的问题),但如果你想正确,你应该不带括号使用它,因为 include 是语句不是函数。

根据PHP include manual page(强调我的):

include 语句包含并评估指定的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2016-09-07
    • 2014-07-30
    • 2015-05-24
    • 1970-01-01
    相关资源
    最近更新 更多