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

This question already has an answer here:

  • Curly braces in string in PHP 5 answers

  • 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) "test"
    

    Or for array:

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

    There is no difference between the following statement -

    echo "This is {$great}";
    echo "This is ${great}";
    

    The output of both statements will be same.Please check following example-

    $great = 'answer';
    echo "This is {$great}"."n";
    echo "This is ${great}";
    
    Output:-
    
    This is answer
    This is answer
    
    链接地址: http://www.djcxy.com/p/59526.html

    上一篇: 什么是{}标签在PHP中做什么?

    下一篇: PHP:{$ foo}和$ {foo}之间是否有区别