What '{$x}' stands for?
This question already has an answer here:
It seems like it was a trick question, first to see if you know what curly braces are used for in a PHP string, but also to to see if you know what the difference between double and single quotes in PHP strings, where single quotes are not evaluated and double quotes are, eg:
$name = "bob";
echo "hello $name"; // hello bob
echo 'hello $name'; // hello $name
I'm guessing they were assuming you would see the curly braces, which are used to isolate a variable within a string and give that answer without looking at the quotes:
$place = 3;
echo "you are {$place}rd in the line"; // you are 3rd in the line
echo "you are $placerd in the line"; // error, $placerd is not defined
echo 'you are {$place}rd in the line'; // you are {$place}rd in the line
When you use single quotes ( ' ) - like in this scenario: echo '{$x}';
, The Variable $x
is not affected in any way. This means that everything within the single quotes would be interpreted as string and echo
ed back verbatim . However, if you had used double quotes instead of single quotes like so: echo "{$x}";
, this would rather result in the evaluation of the value of $x
and the result would be echo
ed. See the Documentation for more information.
上一篇: PHP语法$ {“field”}
下一篇: 什么'{$ x}'代表?