Whats the difference between {$var} and $var?

后端 未结 5 1596
日久生厌
日久生厌 2021-01-19 10:15

I would like to know when and why should I use {$var}

echo \"This is a test using {$var}\";

and when (and why) should I use the simple form

5条回答
  •  抹茶落季
    2021-01-19 10:34

    The brackets allow you to remove ambiguity for the PHP parser in some special cases. In your case, they are equivalent.

    But consider this one:

    $foobar = 'hello';
    $foo = 'foo';
    echo "${$foo . 'bar'}"; // hello
    

    Without the brackets, you will not get the expected result:

    echo "$$foo . 'bar'"; // $foo . 'bar'
    

    For clarity purposes, I would however strongly advise against this syntax.

提交回复
热议问题