Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use parenthesis in require/include statements? [duplicate]

Tags:

php

joomla

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?

like image 969
SomeKittens Avatar asked Jun 06 '12 16:06

SomeKittens


Video Answer


2 Answers

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.

like image 172
Nishu Tayal Avatar answered Nov 14 '22 21:11

Nishu Tayal


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.

like image 23
Jocelyn Avatar answered Nov 14 '22 22:11

Jocelyn