我注意到有一个细微的差别......如果我要编写这样的:
echo "Welcome, $_SESSION['username'], you are logged in.";
它将在解析失败。然而,如果我的代码是这样的:
echo "Welcome, $_SESSION[username], you are logged in.";
它按预期工作,这让我想知道单引号是否真的有必要吗?我无法在PHP文档中找到显示该效果的任何内容。
我注意到有一个细微的差别......如果我要编写这样的:
echo "Welcome, $_SESSION['username'], you are logged in.";
它将在解析失败。然而,如果我的代码是这样的:
echo "Welcome, $_SESSION[username], you are logged in.";
它按预期工作,这让我想知道单引号是否真的有必要吗?我无法在PHP文档中找到显示该效果的任何内容。
在PHP中,没有被定义becomes a string一个全局常量。
Don't rely on this; 总是引用您的数组密钥。
但是,插入字符串,it is fine,因为它已经是一个字符串。
Konforce在关于在字符串插值中使用大括号的注释中提供了一个很好的观点。
如果你忽略它们,don't quote the key。
如果使用它们,you must quote the key,否则常数将为looked up。
请检查this文件
在部分Array do's and don'ts
<?php
// Show all errors
error_reporting(E_ALL);
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
// Correct
print $arr['fruit']; // apple
print $arr['veggie']; // carrot
// Incorrect. This works but also throws a PHP error of level E_NOTICE because
// of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple
// This defines a constant to demonstrate what's going on. The value 'veggie'
// is assigned to a constant named fruit.
define('fruit', 'veggie');
// Notice the difference now
print $arr['fruit']; // apple
print $arr[fruit]; // carrot
// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]"; // Hello apple
// 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']";
// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
这种方式是错误的,但工作$_SESSION[username]
,并采取更多的时间来解析联想指数的价值。
这种影响PHP性能
始终在用字符串字面 数组索引报价。例如, $ foo ['bar']是正确的,而 $ foo [bar]不是。这是错误的,但 它的工作原理。原因在于,此代码 与字符串('bar' - 注意 引号)相比,具有未定义常量(bar)而不是 .PHP可能在将来定义常数,但不幸的是,这些代码具有相同的名称。它的工作原理是PHP自动将一个裸露的字符串(不与任何已知符号对应的未加引号的字符串)转换为包含裸露字符串的字符串。例如,如果没有定义的常量bar,那么PHP将替换字符串'bar'并使用它。
您应该在访问值时使用引号。
我想你没有注意到他在双引号字符串中使用变量。 – ThiefMaster 2011-06-08 06:29:57
@ThiefMaster:这里没关系。他在问为什么应该使用引号访问数组值,即使该引用没有引用 – 2011-06-08 06:35:37
如果** downvoter **发表评论 – 2011-06-08 06:40:19
一个字符串里面你有省略单引号或整个包住变量{}
("...{$array['key']}..."
或...$array[key]...
)。但是,强烈建议在包含"...$foobar..."
之类的内容时避免出现问题"...{$foo}bar..."
(即变量$foo
后跟bar
)。
但你可能不希望在字符串使用瓦尔可言,但正常结束的字符串:'...' . $var . '...'
@Daric:也许你应该在downvoting之前了解答案...... – ThiefMaster 2011-06-08 07:12:57
它被称为裸露的字符串,勉强,在array documentation。如果没有发现与裸字符串匹配的常量 - 由于历史原因,它被认为是字符串文字。然而,这种语法带有很多我不会涉及的语法问题,而且可读性在这里也是一个问题。读者自问 - 这是一个常数还是一个字符串?
现代PHP版本针对此语法发出警告,以便通过使用单引号字符串('username')来解决此问题。
是的。
如果你为一个没有引号的数组传递一个参数,php会首先尝试将该参数解释为一个常量,如果它没有被定义,它将按预期行事。尽管它可以给出相同的结果,但它引用的论证显着较慢。
下面是当这可能无法正常工作的例子:
define("a_constant","a value");
$a = array("a_constant"=>"the right value");
echo $a[a_constant];
的a_constant
变量的值“值”,所以$a[a_constant]
被翻译成$a["a value"]
,不数组$a
中存在的关键。
通常情况下,您必须在字符串内部使用'{$ _SESSION ['username']}'(php文档清楚地说明了这一点)。除此之外,所有输出到html页面的变量都应该使用['htmlspecialchars'](http://php.net/htmlspecialchars) – knittl 2011-06-08 06:27:18
转义。请参阅http://php.net/manual/en/language.types.string .PHP。在“复杂的卷曲”部分,它解释了为什么它不好。 – Matthew 2011-06-08 06:31:39
经验法则,如果您使用变量突出字符串并连接 – 2011-06-08 06:32:08