PHP parser: braces around variables

后端 未结 3 1348
清酒与你
清酒与你 2021-01-05 13:30

I was wondering, how is the semantics of braces exactly defined inside PHP? For instance, suppose we have defined:

$a = \"foo\";

then what

3条回答
  •  走了就别回头了
    2021-01-05 14:03

    The brackets delimit where the variable name ends; this example should speak for itself.

    $a = "hi!"
    
    echo "$afoo";  //$afoo is undefined
    
    echo "${a}foo";  //hi!foo
    
    echo "{$a}foo";  //hi!foo
    

    Also, this should spit out a warning; you should use

    ${'a'}
    

    Otherwise it will attempt to assume a is a constant.

提交回复
热议问题