PHP: Is there a difference between {$foo} and ${foo}

It seems, there is no difference in any PHP version

    $foo = 'test';      
    var_dump("$foo");
    var_dump("{$foo}");
    var_dump("${foo}");

Test: https://3v4l.org/vMO2D

Anyway I do prefer "{$foo}" since I think it's more readable and works in many other cases where other syntax doesn't.

As an example let's try with object property accessing:

var_dump("$foo->bar"); //syntax error
var_dump("{$foo->bar}"); // works great
var_dump("${foo->bar}"); //syntax error

The same case are arrays.

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex


No, there is no differentce.

// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";

Php manual

Answer on stackoverflow

Another way use it for variable:

$foo = 'test';
$test = 'foo';
var_dump("{${$foo}}"); //string(3) "foo"

Or for array:

$foo = ['foo','test'];
var_dump("{$foo[0]}"); //string(3) "foo"
var_dump("${foo[1]}"); //string(4) "test"

Tags:

Php