In the PHP language specifications, the Function calls section mentions:
A function is called via the function-call operator ().
The syntax of a Function Call Expression is:
function-call-expression:
qualified-name ( argument-expression-list opt )
callable-expression ( argument-expression-list opt )
As you can see, a function call expression is made from function name (either the name as a token or a PHP expression that evaluates to it) followed by open parenthesis ((
), an optional list of arguments (separated by comma (,
)), followed by the close parenthesis ()
).
The parentheses are required and they allow the compiler tell apart a constant from a call of a function without arguments. They also allow it to understand that $var()
is a function call while $var
is not.
Have anyone managed to create a custom PHP function where by passing arguments / parameter wihtout braces, similar to something like echo
?
The short answer is "No, this is not possible in PHP". Above is explained why not.
echo
is not a function, it is a language construct. The same for print
, include
/include_once
/require
/require_once
.
Using echo
with parenthesis is possible but, in fact, the parenthesis are not part of the echo
, they are part of the expression that is printed by echo
.
The following code is invalid:
echo('one', ' ', 'two');
It would be valid if echo
were a function.
Look at this code:
echo('one'), ' ', ('two');
This is apparently invalid PHP error code. And it would be invalid if echo
were a function. But echo
is not a function and the code is valid.
Let's assume, for a moment, that echo
is a function and the language allows calling functions with or without parentheses. The line above is ambiguous. What are the arguments of echo
?
Should the compiler think the call to echo
is echo('one')
or ('one')
is just a string in parentheses (a perfectly valid expression) and the arguments of echo
are ('one')
, ' '
and ('two')
?