Why does "echo" accept parentheses in PHP?

Asked

Viewed 317 times

4

In PHP, the echo can you receive parentheses because you consider that an expression? Apparently, some language features do not seem to be standardized and can therefore be used in numerous ways.

For example:

echo ('teste');

Or else:

echo ('teste1'), ('teste2');

I would like to know why this is accepted and why echo accept commas. I would also like to know if echo is or is not a language construct, as the documentation says the following:

echo is not a function currently (language constructor) so it is not required to use parentheses.

2 answers

4

The question already answers. PHP is a language without much discretion. It was created by those who do not understand language theory and do it in need (nor is it possible to speak in intuitivity because the intuitive is the standard). And whoever creates the language can do as they please, even without having to explain to anyone why it was done that way, even though it makes the language lose credibility among good development professionals.

We can see echo in two ways: a construction of language, since it accepts without parentheses, therefore it is special, it is not a common function; or we can see as a function, which is inherently an expression always, and the documentation helps to refer to this thesis, even if it indicates not to be. In the end there was a confusion between one thing and another. Because it always results in void makes little sense to use echo as an expression.

And what you pass as a parameter can be any expression that can be converted to the type string.

Whether or not to have parentheses is a decision of the creator of language. I would make it a common function obligatory of them. But if I really want to make it be language construction I would not allow its use under normal conditions to make it clear that it is a construction and not a function, although it is a little complicated but not impossible, prohibit the parentheses of an expression like any other.

It is not clear what these parentheses refer to, and in practice it matters little, but I would say that they are part of the construction and not an expression, because in expressions the parentheses in such a case are unnecessary, so it would make no sense to document. But again, it’s speculation from someone who studies languages. At least in the first example. In the second it is clearly of expression, because the parentheses of the construction can only contain all expression to be echoed, the fact of having 2 removes the ambiguity of the interpretation.

The construction accepts commas because it was set it would accept. It makes sense it can have multiple expressions and it handle it and strain all.

2


Is not the echo that accepts parentheses, what happens is that everything that comes afterward of echo with or without parentheses will be understood as values, for example one or more strings or values or variables that can be "printable")

So much so that echo supports multiple values, for example:

echo 'a', 'b', 'c';

or:

 echo ('a'), ('b'), ('c');

If I did that I would fail:

 echo ('a', 'b', 'c');

Causing the error:

Parse error: syntax error, Unexpected ','

It works this way because echo is not a function, but rather a language builder.

I find that term a little strange, according to the documentation, but I understand that this means that the echo is something "natural" in the PHP language, as if it were if, else, break, etc. That is to say echo is part of the language and behaves as needed and cannot actually be executed as a function.

Now understanding parentheses, in PHP (and even in most languages) they are used to "isolate" certain operations, like a mathematical operation in PHP so would return a value:

$foo = 20 + 8 * 2; // 36
echo $foo;

That would be another:

$foo = (20 + 8) * 2; // 56
echo $foo;

Namely the echo or even set a variable ($foo =) do not see the parentheses, the language itself evaluates this before and the parentheses are used to separate necessary operations, such as mathematical operations

In short, do it echo ('foo'); would almost be equivalent to doing this:

$a = ('foo');
echo $a;

For parentheses are not part of echo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.