Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple assignments to the same variable

Why do I get a parse error with this code:

$func = "do_{$something}" = $func();

?

It should be correct because

$func = "do_{$something}";
$func = $func();

works...

like image 835
Alex Avatar asked Dec 10 '22 03:12

Alex


1 Answers

Because the assignment works from right to left.

Look at this code as an example:

$a = $b = 3;

If assignment would work from the left, this'll be parsed as:

$a = $b;
$b = 3;

which would give you an undefined variable error.

Instead, it's parsed as:

$b = 3;
$a = $b;
like image 174
Joseph Silber Avatar answered Dec 27 '22 00:12

Joseph Silber