什么时候应该在 require/include 语句中使用括号?

2022-01-06 00:00:00 php joomla

我收到了一堆代码,其中包含很多 require/include 语句(混合在 require 之间)require_once).有时,路径周围有括号,即require_once (JPATH_COMPONENT.DS.'controller.php');,有时则没有:require_once $path;.

I've been handed a pile of code that includes a lot of require/include statments (mixed between require and require_once). Sometimes, the path has parenthesis around it, i.e. require_once (JPATH_COMPONENT.DS.'controller.php');, and other times there isn't: require_once $path;.

包括 提到这一点的 php 文档,但不是具体的.当我找到它们时,我应该删除括号,还是可以不理会它们?在编写进一步的 require/include 语句时,我应该在哪些特定情况下使用它们?

The php docs for include mention this, but aren't specific. Should I remove the parenthesis when I find them, or is it ok to leave them alone? When writing further require/include statements, are there specific cases where I should use them?

推荐答案

你被允许在 'include/require' 中使用括号不是因为 include 允许它本身而是因为你可以在 PHP 中的任何字符串或数字周围使用括号进行分组.

You are allowed to use parentheses in 'include/require' not because include allows it itself but because you can use parentheses around any string or number in PHP for grouping.

例如,"dog" 等价于 ("dog")("dog")."dog" 是相当于"dog"."dog"

So for example, "dog" is equivalent to ("dog"), ("dog")."dog" is equivalent to "dog"."dog", etc.

当您使用涉及计算和字符串连接的复杂表达式时,括号会变得很有用,但在这种简单的情况下,它们只是被允许使用并对单个字符串值执行不必要且无害的分组".

Parentheses become useful when you use complex expressions involving calculations and string concatenations but in such a simple case, they are simply allowed and perform an unnecessary and harmless "grouping" of a single string value.

相关文章