Have you seen PHP functions used without parentheses, such as echo or include? That’s because these aren’t really functions. They are a short list of entities called language constructs.

Below is a list of PHP language constructs. Half of these can be used with or without parentheses. It is entirely optional and that is why you have seen it done both ways.

Using parentheses will make your code slightly more consistent and readable. It would also be slightly less confusing to those who read it. After all, you ended up on this article as well!

According to PHP.net, not using parentheses will make your code slightly faster, because it means PHP has to do a little less work. In testing, however, this is not the case. Consider one of my speed tests:

<?php
    $time_start = microtime(true);
    for($i=0;$i<250000;$i++){
        // try this include with or without ()
        $y = include "test.txt";
    }
    $time_end = microtime(true);
    echo $time_end - $time_start;
?>

Over a loop with a quarter-million iterations, the include statement with and without parenthesis would execute at the same speed of 27-28 seconds. In a second test using twenty five million iterations, a blank echo would take 4.2 seconds either way. Therefore, the decision is easy.

Solid Statement: Always use parentheses with language constructs. It makes your code more readable and consistent, without sacrificing speed.

List of PHP Language Constructs

Parentheses Optional

 

Parentheses Required

  • echo()
  • include()
  • include_once()
  • require()
  • require_once()
  • return()
  • print()
  • die()
  • exit()
  • eval()
  • empty()
  • isset()
  • list()
  • unset()