Why does "echo" accept parentheses in PHP?

3

In PHP, can echo receive parentheses because it considers this an expression? Apparently, some language features do not seem to be standardized and therefore can be used in a number of ways.

For example:

echo ('teste');

Or else:

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

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

  

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

    
asked by anonymous 21.11.2018 / 16:02

1 answer

5

The question already answers. PHP is a language without much criteria. It was created by those who do not understand language theory and are doing it in need (nor can you speak in intuitiveness because the intuitive is the standard). And whoever creates the language can do it any way they want, even without having to explain it to anyone because 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 language construct, since it accepts unrelated, so it is special, it is not a common function; or we can see how a function, which is inherently an expression always, and the documentation helps to refer to this thesis , even if it indicates that it is not. In the end there was a confusion between one thing and another. Because it always results in void it 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 type string .

Whether or not you can have parentheses is a decision of the creator of language. I would make this a common function with binding of them. But if I really want to be a language construct, I would not allow it to be used under normal conditions to make it clear that it is a construct, not a function, although it is somewhat complicated but not impossible to forbid the parentheses of an expression like any other.

It is not clear what these relatives refer to, and in practice it does not matter, but I would say that they are part of the construction and not an expression, even because in expressions the parentheses in such cases are unnecessary, then it would not make sense to document. But again, it is speculation of 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 every expression to be echoed, the fact of having 2 removes the ambiguity of the interpretation.

The construct accepts commas because it was defined that it would accept. It makes sense can have multiple expressions and it handle it and coar all.

    
21.11.2018 / 16:20