2

I apologize in advance if this is a needless question.

I've looked around for syntax on php echo and require functions, and multiple pages have different answers. However, each version of the syntax works.

What I want to know is if any of these versions are obsolete, or if one performs slightly better than the other.

<?php echo($variable); ?>
<?php echo $variable ?>

<?php require('file.php'); ?>
<?php require 'file.php'; ?>

I see them with parentheses, without parentheses, with single quotation marks, without.. etc.

Is there a definitive syntax for each one?

Thank you.

4
  • 1
    In these cases, parentheses are just for formatting. You may also want to look into require_once Commented Mar 4, 2013 at 0:01
  • 1
    "or if one performs slightly better than the other". Microperformance? Don't worry about it, no parenthesis looks cleaner, I'd just go with that. Commented Mar 4, 2013 at 0:03
  • Can you explain what you mean by formatting? And thank you for the suggestion with require_once, was actually just reading about it on php.net Commented Mar 4, 2013 at 0:04
  • @popsicle As mentioned in one of the answers, neither echo or require need parentheses. See Phil's answer below. Commented Mar 4, 2013 at 0:06

3 Answers 3

3

From the PHP docs on echo

echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.


Both echo and require are language constructs, the same applies to require, include, require_once and include_path.

Sign up to request clarification or add additional context in comments.

9 Comments

-0.5 And what about require?
@AarolamaBluenk Edited reply.
The important thing to mention here is that both functions use parentheses when passing more than one variable to them.
@AarolamaBluenk false. echo 1, 2, 3, 4; is perfectly valid.
@popsicle No. In case of echo, as mentioned in my reply above, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses. As an example, see this link. Now, as for require and require_once; they can have atmost one argument passed to them.
|
3

The parentheses do make a difference. All you need to do is read the fine manual

echo

Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses

include (applies to require as well)

Because include is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.

See Example #4 for how it can make a difference.

I would recommend not using parentheses with either construct due to the limitations they impose (echo) and the effect on return values (include)

1 Comment

The only complete and thorough answer. +1
3

echo and require aren't functions, they're language constructs which don't require parenthesis.

There's no notable performance differences.

4 Comments

This does not completely answer the question. "What I want to know is if any of these versions are obsolete, or if one performs slightly better than the other."
Why not? If () are not required, then they are optional.
How does that excerpt from the manual apply to the performance?
It doesn't, it was poor grammar. Why do you feel the need to comment and down vote every answer in this thread when you don't know what you're talking about anyway?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.