Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between single and double quotes for PHP array key values_PHP Tutorial

Detailed explanation of the difference between single and double quotes for PHP array key values_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:11870browse

When we write to traverse the array, there are several methods, such as directly $arr['key'] and $arr["key"] and $arr[key]. We can all display the relevant content, but their difference is Where is it? Let me introduce it below.

1. The difference between $arr['key'] and $arr["key"] and $arr[key]:

The above three methods mainly access the value of the array through the string type array subscript, that is, the array key. If the array subscript is an index type, that is, the key value is a number, there is no need to pay attention.

1, $arr['key'] single quote pattern is directly parsed into the value of $arr;
2. The $arr["key"] double quote mode will first analyze whether the "key" string contains a PHP variable, and then parse it into the value of $arr;
3. If $arr[key] does not have any quotes, it will first analyze whether there is a key constant definition in the local scope (that is, whether there is define('key','val')),

If there is one, use the constant value represented by the local key constant as the array key value;
Otherwise, continue to analyze whether there is a key constant definition in the global scope,
If there is, use the constant value represented by the global key constant as the array key value;
Otherwise key is converted internally to the 'key' string scalar value and an E_NOTICE exception is thrown.

2. The difference between $arr["$str_key"] and $arr[$str_key]

This method also accesses the value of the array through the string type array subscript,
If the array subscript is an index type, that is, a number, there is no need to pay attention.
In fact, there is no need to add additional double quotes here to indicate that the $str_key variable represents a string value,

i.e. $arr["$str_key"] === $arr[$str_key]

The code is as follows
 代码如下 复制代码

define('constant','arr1');
$constant = 'arr2';
$variable = 'arr1';
$arr = array(
'arr1'=>'arr1', 
   'arr2'=>'arr2', 
   'arr3'=>'arr3', 
);
 
echo $arr['arr1'],'
',$arr["$variable"],'
',$arr[constant],'
',$arr[$constant];
 
?>

Copy code
define('constant','arr1');

$constant = 'arr2';

$variable = 'arr1'; 'arr1'=>'arr1', 'arr2'=>'arr2', 'arr3'=>'arr3', ); echo $arr['arr1'],'
',$arr["$variable"],'
',$arr[constant],'
' ,$arr[$constant];
?>
http://www.bkjia.com/PHPjc/632804.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632804.htmlTechArticleWhen we write to traverse the array, there are several methods, such as direct $arr['key'] and $arr[" key"] and $arr[key], we can both display the key content, but what is the difference between them? Let me...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn