What is the difference between echo with braces and without braces, and why do both methods exist?
Asked Answered
G

3

8

I think most of us that program in php learned to echo "string";. While common, I am wondering why we use this so different then any other function.

So why do we do:

echo "Some String";

Instead of

echo("Some String");

And why do both exist and behave differently? Is there any reference to why this choice was made?

People refer to the php docs stating that echo is a language construct and therefor is used differently. But in that case, since we can both use it as a construct aswell as functional: which is the preferred method? And why was it implemented both ways while it should only be a language construct?

The same as above pretty much counts for require, require_once, include and include_once. I can't find anything on the web explaining -why- these constructs were also implemented functional (and in the case of echo(), in a flawed way).

Germane answered 22/5, 2013 at 9:36 Comment(2)
S
13

From php.net:

echo is not actually a function (it is a language construct), so you are not required to use parentheses with it.

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

Example:

<?php
echo(1);        // < works fine
echo(1, 2, 3);  // < Parse error: syntax error, unexpected ',' on line 3
echo 1;         // < works fine
echo 1, 2, 3;   // < works fine
echo (1), (2), (3);   // < works fine

Regarding your question "why do both methods exist?", using braces is not actually a "method". Those braces do not belong to echo, but to its argument. In PHP, almost any expression can be taken in braces. For example, ($a = 1); and $a = (1) are valid statements. So it's just an extra pair of braces. As well as in trim(($str));, where first pair of braces belongs to the function and the second pair - to its argument. So now you can tell that using echo with braces is as logical as using trim() with 2 pairs of braces.

Everything said above is also applied to include, require, require_once and include_once, as well as other language constructs, but with some exceptions. For example, isset, although being a language construct, for some reason requires a pair of braces.

Note that, as it's mentioned on the manual page for include, these language constructs have a return value. And because expressions like include 'fileA.php' == 'OK' are not obvious (according to operator precedence), when checking for the return value, you should use parentheses, either wrapping the entire expression: (include 'fileA.php') == 'OK' or the argument only: include('fileA.php') == 'OK'.

Saiva answered 22/5, 2013 at 9:37 Comment(8)
Best answer so far, thanks @Corrupt. I'm still wondering why the developers made the "language construct" usable in a functional way though. Especially since it behaves differently. Is this just a "fluke" in the php dev history? Or was there a good reason behind it?Germane
@Damien Overeem , backward capability issues, maybe.Saiva
Seems this is not considered a "real question". Which is odd if you ask me, but whatever. I would have loved to find out the reasoning behind this kinda strang behavior... I'll accept your answer though, since it did point out the silly difference between the 2 methods.Germane
@Damien Overeem I've updated answer with test of variable function call.Saiva
Nice catch. Makes the existance of echo(); even more awkward. It works, but has total unexpected behavior...Germane
@Damien Overeem updated to fit requirements of Edit 2.Saiva
Rather interesting. I didn't even know you could return in an include file. Rather nasty thing to actually use though, but interesting to know.Germane
@Damien Overeem Yes, I'm not using it too, but someone somewhere may do it in some incomprehensible way. It is not evUl until it is not in curved hands.Saiva
B
2

From the PHP docs

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

It's not a funcion, hence no parenthesis

Bittencourt answered 22/5, 2013 at 9:37 Comment(0)
T
0

Because is a statement(language construct) it is not a function. Hence to differentiate a function and statement it is used like this.

Tailgate answered 22/5, 2013 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.