40

Is this just a stylistic difference, or does using require_once('filename.php') vs require_once 'filename.php' have actual load/efficiency differences?

2
  • Just an extra character to type in the case of parentheses.
    – bcosca
    Commented Aug 3, 2010 at 8:33
  • I'm getting different behaviour for the two. One path is written using dirname and another works relative to the site root. Very odd.
    – James P.
    Commented Aug 23, 2013 at 1:38

5 Answers 5

38

Pear Coding Standards say :

"include_once and require_once are statements, not functions. Parentheses should not surround the subject filename."

Source : http://pear.php.net/manual/en/standards.including.php

20

It's exactly the same thing. It's a matter of style.

The parentheses may get in the way some times. For instance, this example from the manual doesn't do what you expect:

if (include('vars.php') == 'OK') {
    echo 'OK';
}

See example #4.

2
  • 7
    But this is not caused by the parentheses.
    – Gumbo
    Commented Aug 3, 2010 at 5:57
  • I think you should include in the answer what it really does. ('vars.php') == 'OK' is interpreted as the argument to include like in if (include(('vars.php') == 'OK')) {. That's why you should avoid using parentheses so it doesn't bring you to the wrong conclusion.
    – PhoneixS
    Commented Apr 1, 2022 at 11:36
12

What does your heart tell you?

Performance difference, if any: negligible.

1
  • 4
    I just like that this can be a valid answer to a programming question! Commented Sep 26, 2013 at 12:47
7

There is no difference. I don't use the brackets 'cause they are not necessary. require_once is no function.

2
  • 2
    "is no function" does not imply parentheses are not necessary (see isset, etc.).
    – Artefacto
    Commented Aug 3, 2010 at 5:44
  • That's not my implication - I only mention it ;)
    – dst
    Commented Aug 3, 2010 at 5:45
1

include, include_once, require and require_once are not functions, they are statements, that is why you should not use ().

Also, consider this from php.net:

<?php

// won't work
if (include('vars.php') == TRUE) {
    echo 'OK';
}

// works
if ((include 'vars.php') == TRUE) {
    echo 'OK';
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.