0

I got an unusual question.. When we pass arguments into a PHP function, we always uses round brackets, ie :

function myFunction($argument){ 
  return $argument; 
}

and we call it with

myFunction($argument);

Have anyone managed to create a custom PHP function where by passing arguments / parameter wihtout braces, similar to something like echo.. ie:

myFunction $argument;
  • See https://stackoverflow.com/questions/1180184/what-is-the-difference-between-a-language-construct-and-a-built-in-function-in. – localheinz Oct 22 '17 at 06:34
  • In a function definition you have a list of variables. They are called **parameters**. But, when you are calling a function you are passing a list of values (one for each defined parameter). These values are called **arguments** ;-) –  Oct 22 '17 at 06:40
  • [`echo`](http://php.net/manual/en/function.echo.php) [is not a function](http://php.net/manual/en/function.echo.php#refsect1-function.echo-notes). – axiac Oct 22 '17 at 07:05
  • Not 'real' PHP, but If you really want to try - https://www.sitepoint.com/php-macros-for-fun-and-profit/ may be able to do something for you (Disclaimer - not for beginners, intermediate or advanced users - just for people who don't want to do this sort of thing for a living ;) – Nigel Ren Oct 22 '17 at 07:45

1 Answers1

1

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')?

axiac
  • 68,258
  • 9
  • 99
  • 134