考虑下面的代码:
<?php
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
define('fruit', 'veggie');
print "Hello {$arr['fruit']}"; //This works
print "Hello $arr['fruit']"; //This doesn't work
?>
我无法理解为什么第二个不起作用并给我解析错误,如下所示:
**Parse error:** syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
另外,如果我将上面的程序编写如下,那就是添加 die;在第一条执行线之后,然后放置非工作线,我仍然得到同样的错误。
<?php
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
define('fruit', 'veggie');
print "Hello {$arr['fruit']}";
die;
print "Hello $arr['fruit']";
?>
我根本没有得到这个。它应该已经打印了第一行,因为我在它后面的代码死掉了,在编译时不应该考虑 die 后面的行,但它正在考虑并禁止执行第一行。
为什么这样?