Why embedding functions inside strings is different from variables

I asked such a question before, but it's different, it's more about the parsing logic.

My previous questions were about how to embed a function inside a string (with double quote), and I got this answer:

$date = "date";
echo "This page is under construction<br/><br/>Current Date: {$date('l jS \of F Y')}";

And after that, I started wondering why the one below does not work, while the one above works fine:

echo "This page is under construction<br/><br/>Current Date: {date('l jS \of F Y')}";

Like the logic of the analysis process, even if the variables work pretty well inside the strings.

I read that after the PHP parser sign, $he tries to find the appropriate variable for parsing and running, as well as to delimit the variable name, we also use curly braces {}, and this is also what I understand fairly.

But why does this syntax seem necessary when developing a parser engine for functions, because at first it made no sense to me.

Basically, why do I need to define a variable that contains a string representation of the function name, for example, below:

$date = "date";

Thanks in advance.

+3
source share
2 answers

From the documentation :

Note:

Functions, method calls, static class variables and class constants inside {$} work with PHP 5. However, the available value will be interpreted as the name of the variable in the field that is defined in the line. Using single curly braces ({}) will not work to access the return values ​​of functions or methods or the values ​​of class constants or static class variables.

Here is a hack it though:

function _expression($x) { return $x; }
$e = '_expression';

echo "This page is under construction<br/><br/>Current Date: {$e(date('l jS \of F Y'))}";
+4

. - {$. PHP. T_CURLY_OPEN.

, PHP, .

+2

All Articles