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
|
Copy code | ||||
$constant = 'arr2';