问题
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;
.
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?
回答1:
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.
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.
回答2:
There is no issue with leaving them or taking them out, at the end of the day it is up to the comfort of the developer.
Personally, I leave them off. I think it looks a little cleaner, and the IDE syntax coloring works a bit better.
回答3:
Both syntaxes are valid. So, it's up to you. :)
The documentation explains:
Because include is a special language construct, parentheses are not needed around its argument.
来源:https://stackoverflow.com/questions/10918598/when-should-i-use-parenthesis-in-require-include-statements