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).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Damien Overeem
  • 4,487
  • 4
  • 36
  • 55
  • Related: [What is the difference between a language construct and a "built-in" function in PHP?](https://stackoverflow.com/q/1180184/2943403) – mickmackusa May 31 '23 at 06:42
  • Also related: [PHP: What are language constructs and why do we need them?](https://stackoverflow.com/q/3254327/2943403) – mickmackusa May 31 '23 at 06:51

3 Answers3

12

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'.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • 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? – Damien Overeem May 22 '13 at 09:44
  • @Damien Overeem , backward capability issues, maybe. – BlitZ May 22 '13 at 09:45
  • 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. – Damien Overeem May 22 '13 at 09:49
  • @Damien Overeem I've updated answer with test of _variable function_ call. – BlitZ May 22 '13 at 09:55
  • Nice catch. Makes the existance of `echo();` even more awkward. It works, but has total unexpected behavior... – Damien Overeem May 22 '13 at 09:58
  • @Damien Overeem updated to fit requirements of **Edit 2**. – BlitZ May 22 '13 at 11:43
  • 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. – Damien Overeem May 22 '13 at 11:50
  • @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. – BlitZ May 22 '13 at 11:54
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

fullybaked
  • 4,117
  • 1
  • 24
  • 37
0

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

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45