// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
有过 3 条评论 »
$conn['host'] = '127'; $dsn = "HOST is $conn['host']"; echo $dsn;Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)<?php $conn['host'] = '127'; $t0 = microtime(1); for ($i=0; $i < 9999999; $i++) { $dsn = "HOST is {$conn['host']}"; } $t1 = microtime(1); echo ($t1 - $t0); //1.0226299762726 秒 echo "\n"; for ($i=0; $i < 9999999; $i++) { $dsn = "HOST is $conn[host]"; } $t2 = microtime(1); echo ($t2 - $t1); //1.0196290016174 秒// With one exception: braces surrounding arrays within strings allows constants // to be interpreted print "Hello {$arr[fruit]}"; // Hello carrot print "Hello {$arr['fruit']}"; // Hello apple // This will not work, and will result in a parse error, such as: // Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' // This of course applies to using superglobals in strings as well print "Hello $arr['fruit']"; print "Hello $_GET['foo']";echo 'HOST is ', $conn['host'];- 用逗号连接,不拼接字符串,也不用双引号解析变量。省资源。
echo (int)'abc1'; //输出0 echo (int)'1abc'; //输出1 echo (int)'2abc'; //输出2 echo (int)'22abc'; //输出22“逗号”并不是一个连接符,他只是在使用 echo 输出一系列的变量、字符串、数字等等内容时使用,或者说通过 echo 输出多个东西时,用“逗号”分割开。
所以,“逗号”和“点号”没有什么联系,也没有什么可对比的。
echo 'a' . 'b'. 'c'; 是将三个字符串拼接之后输出 echo 'a', 'b', 'c'; 是依次输出三个字符串省去了字符串拼接的步骤,理论上在 echo 的时候用“逗号”性能会高一点。
不带引号,host 表示一个常量,PHP 会先去找这个常量,如果有就用其值作为键值,如果没有就把 host 看作字符串直接作为键值,并报出一个 Notice
`Notice: Use of undefined constant host - assumed 'host'....`
$dsn = "HOST is {$conn['host']}";
要不
$dsn = "HOST is $conn[host]";
在双引号里面访问数组数据可以不加单引号。
如果我们给A的事情增加修饰符:
public事件,地球人都知道,全公开
protected事件,A,B,D知道(A和他的所有儿子知道,妻子C不知道)
private事件,只有A知道(隐私?心事?)
internal事件,A,B,C知道(A家里人都知道,私生子D不知道)
protected internal事件,A,B,C,D都知道,其它人不知道