2016-08-18 126 views
0

我不得不接受一个关于php的测试来评估我在现场的知识。作为这种语言的初学者,有些问题很难回答。

其中之一是告诉,echo '{$x}';的输出是什么?由于我从来没有在功能调用之外看到过这种括号,所以我不知道。

你能说出来吗? echo '{$x}';,变量$x没有受到任何影响:在这种情况类似 -

+5

由于您使用的是单引号,因此它会回显'{$ x}'。 – Daan

+4

[检查出来](http://stackoverflow.com/questions/2596837/curly-braces-in-string-in-php) –

+1

还检查了单引号字符串和双引号字符串之间的区别[PHP的Dcs]( http://www.php.net/manual/en/language.types.string.php) –

回答

2

看起来这是一个有趣的问题,先来看看,如果你知道一个PHP字符串,使用什么花括号的,但也来,看看你知道什么是双和单引号的区别在PHP中的字符串,其中单引号不被评估和双引号,例如:

$name = "bob"; 
echo "hello $name"; // hello bob 
echo 'hello $name'; // hello $name 

我猜他们是假设你会看到大括号,这是用来在字符串中的变量隔离,并给予回答时不用看报价:

$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 
+0

所以''你是{$ place} rd在行中''等于''你是'。$ place。“rd在行中''? –

+0

是的输出将完全相同。花括号仅用于在双引号字符串内分隔(隔离)PHP变量,因此在上面的示例中将变量('$ place')与字符串的“rd”部分分开 –

1

当您使用单引号()。这意味着单引号内的所有内容都将被解释为字符串并且echo ed back verbatim。 但是,如果您使用双引号而不是单引号,例如:echo "{$x}";,则宁可导致对$x的值的评估,结果将为echo ed。请参阅Documentation了解更多信息。