在访问 PHP 中的数组元素时,是否必须在索引周围使用引号(单引号和双引号)?
Is it mandatory to use quotes(single quotes & double quotes) around an index while accessing array elements in PHP?
我想要这个问题的权威答案,或者你可以说我的问题。
让我们看一下以下三个代码片段:
第一个:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y;
?>
</body>
</html>
第二个:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS["y"] = $GLOBALS["x"] + $GLOBALS["y"];
}
myTest();
echo $y;
?>
</body>
</html>
第三个:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS[y] = $GLOBALS[x] + $GLOBALS[y];
}
myTest();
echo $y;
?>
</body>
</html>
对于上面的每个代码片段,我在浏览器中得到了相同的结果 15.
如果您仔细观察以上三个代码片段,您会发现以下三个不同的语句:
//From First Code Snippet. Here I've used single quotes around the array index.
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
//From Second Code Snippet. Here I've used double quotes around the array index.
$GLOBALS["y"] = $GLOBALS["x"] + $GLOBALS["y"];
//From Third Code Snippet. Here I've not used any kind of quotes around the array index.
$GLOBALS[y] = $GLOBALS[x] + $GLOBALS[y];
所以,我的疑惑如下:
- 访问特定数组元素时是否必须在数组索引周围使用引号?
- 如果是,那么是哪种类型的引号,我的意思是单引号还是双引号?
- 尽管我得到了相同的结果,但我在上一个代码片段中没有使用任何类型的引号。这是否意味着在访问数组元素时使用引号不是强制性的?
- 或者这是仅对
$GLOBALS
数组有效的特殊情况?
在PHP中,单引号和双引号的行为几乎相同。唯一的例外是双引号允许转义。
$GLOBALS["y"]
和 $GLOBALS['y']
在这种情况下是相同的。
$GLOBALS[y]
可能有效,但有问题。 y
引用名为 y
的常量变量。如果你没有,它会被解析为一个字符串,因此像上面一样工作。
关于您的问题
但实际上是错误的,你应该使用引号!
Is it mandatory to use quotes around the array index while accessing the particular array element?
绝对。
If yes then which type of quotes, I mean single quotes or double quotes?
就像我说的,你可以使用任何一个。我个人更喜欢单一的 ('
),但这取决于你。
I've not used any kind of quotes in my last code snippet though I got the same result. Does that mean using quotes while accessing the array elements is not mandatory?
查看问题 #1 的答案。
Or is this the special case which is valid only for $GLOBALS array?
它被解释为定义为 const y = '...';
的常量变量
我想要这个问题的权威答案,或者你可以说我的问题。
让我们看一下以下三个代码片段:
第一个:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y;
?>
</body>
</html>
第二个:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS["y"] = $GLOBALS["x"] + $GLOBALS["y"];
}
myTest();
echo $y;
?>
</body>
</html>
第三个:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS[y] = $GLOBALS[x] + $GLOBALS[y];
}
myTest();
echo $y;
?>
</body>
</html>
对于上面的每个代码片段,我在浏览器中得到了相同的结果 15.
如果您仔细观察以上三个代码片段,您会发现以下三个不同的语句:
//From First Code Snippet. Here I've used single quotes around the array index.
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
//From Second Code Snippet. Here I've used double quotes around the array index.
$GLOBALS["y"] = $GLOBALS["x"] + $GLOBALS["y"];
//From Third Code Snippet. Here I've not used any kind of quotes around the array index.
$GLOBALS[y] = $GLOBALS[x] + $GLOBALS[y];
所以,我的疑惑如下:
- 访问特定数组元素时是否必须在数组索引周围使用引号?
- 如果是,那么是哪种类型的引号,我的意思是单引号还是双引号?
- 尽管我得到了相同的结果,但我在上一个代码片段中没有使用任何类型的引号。这是否意味着在访问数组元素时使用引号不是强制性的?
- 或者这是仅对
$GLOBALS
数组有效的特殊情况?
在PHP中,单引号和双引号的行为几乎相同。唯一的例外是双引号允许转义。
$GLOBALS["y"]
和 $GLOBALS['y']
在这种情况下是相同的。
$GLOBALS[y]
可能有效,但有问题。 y
引用名为 y
的常量变量。如果你没有,它会被解析为一个字符串,因此像上面一样工作。
关于您的问题
但实际上是错误的,你应该使用引号!
Is it mandatory to use quotes around the array index while accessing the particular array element?
绝对。
If yes then which type of quotes, I mean single quotes or double quotes?
就像我说的,你可以使用任何一个。我个人更喜欢单一的 ('
),但这取决于你。
I've not used any kind of quotes in my last code snippet though I got the same result. Does that mean using quotes while accessing the array elements is not mandatory?
查看问题 #1 的答案。
Or is this the special case which is valid only for $GLOBALS array?
它被解释为定义为 const y = '...';