When should parentheses be used in require / include statements?

I was given a bunch of code that includes many require / include statuses (mixed between require and require_once ). Sometimes a path has a parenthesis around it, i.e. require_once (JPATH_COMPONENT.DS.'controller.php'); , and in other cases not: require_once $path; .

The php docs for include mention this , but they are not specific. Should I remove the brackets when I find them, or is it okay to leave them alone? When writing additional require / include queries, are there specific cases when should I use them?

+6
source share
3 answers

You can use parentheses in 'include / require' not because include allows it yourself, but because you can use parentheses around any string or number in PHP to group.

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

Brackets become useful when you use complex expressions that include calculations and concatenations of strings, but in this simple case they are simply resolved and perform an unnecessary and harmless β€œgrouping” of a single string value.

+18
source

There is no problem to leave them or pull them out, in the end, it depends on the convenience of the developer.

Personally, I leave them. I think it looks a little cleaner and the IDE syntax coloring works a little better.

+1
source

Both syntaxes are valid. So it is up to you. :)
The documentation explains:

Since include is a special language construct, brackets are not needed around the argument.

0
source

Source: https://habr.com/ru/post/917465/


All Articles