Differences between single and double quotation marks of PHP array key values

Source: Internet
Author: User
Tags constant definition

I. Differences between $ arr ['key'] and $ arr ["key"] and $ arr [key:

The above three methods mainly access the array value 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, you do not need to pay attention to it.

1, $ arr ['key'] The single quotation mark mode is directly parsed as the value of $ arr;
2. $ arr ["key"] in double quotation mark mode, the system first analyzes whether the "key" string contains the PHP variable and then parses it into the value of $ arr;
3, $ arr [key] without any quotation marks, it will first analyze whether there is a key constant definition in the local scope (that is, there is no define ('key', 'Val ')),

If yes, use the constant value represented by a local key constant as the array key value;
Otherwise, continue to analyze whether the key constant definition is in the global scope,
If yes, the constant value represented by the global key constant is used as the array key value;
Otherwise, an E_NOTICE exception is thrown when the internal conversion key is a 'key' string scalar value.

II. Differences between $ arr ["$ str_key"] and $ arr [$ str_key]

This method also accesses the value of the array through the subscript of the string type array,
If the array subscript is an index, that is, a number, you do not need to pay attention to it.
In fact, no extra double quotation marks are required to indicate that the $ str_key variable represents the string value,

That is, $ arr ["$ str_key"] ===$ arr [$ str_key]

The code is as follows: Copy code

<? Php
Define ('constant', 'arr1 ');
$ Constant = 'arr2 ';
$ Variable = 'arr1 ';
$ Arr = array (
'Arr1' => 'arr1 ',
'Arr2 '=> 'arr2 ',
'Arr3 '=> 'arr3 ',
Echo $ arr ['arr1 '],' <br/> ', $ arr ["$ variable"],' <br/> ', $ arr [constant], '<br/>', $ arr [$ constant];

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.