PHP array key value of single double quotation mark difference detailed _php tutorial

Source: Internet
Author: User
Tags constant definition
There are several ways we write traversal arrays, such as direct $arr[\ ' key\ ' and $arr [\ ' key\ '] and $arr [key] We can all show off the content, but where do they differ, let me introduce you.

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

The above three ways are mainly through the string type array subscript is the array key to access the value of the array, if the array subscript index type is the key value of the number is not to be aware.

1, $arr [' key '] single quote mode is parsed directly to the value of $arr;
2, $arr ["key"] double quote mode will first analyze whether the "key" string contains PHP variables, and then resolves to $arr values;
3, $arr [key] without any quotation marks will first analyze whether there is a key constant definition in the local scope (that is, there is no define (' key ', ' Val ')),

If any, use the constant value represented by the local key constant as the array key value;
Otherwise, there is no key constant definition in the global scope,
If any, the constant value represented by the global key constant is used as the array key value;
Otherwise the internal conversion key is the ' key ' string scalar value and throws a E_notice exception.

Ii. differences between $arr ["$str _key"] and $arr [$str _key]

This method also accesses the value of the array through the array of string type arrays,
If the array is indexed as an index type then the number is not to be noticed.
In fact, there is no need to add double quotation marks 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

Define (' constant ', ' arr1 ');
$constant = ' arr2 ';
$variable = ' arr1 ';
$arr = Array (
' arr1 ' = ' arr1 ',
' arr2 ' = ' arr2 ',
' Arr3 ' = ' arr3 ',
);

echo $arr [' arr1 '], '
', $arr ["$variable"], '
', $arr [constant], '
', $arr [$constant];

?>

http://www.bkjia.com/PHPjc/632804.html www.bkjia.com true http://www.bkjia.com/PHPjc/632804.html techarticle There are several ways we write traversal arrays, such as direct $arr[\ ' key\ ' and $arr [\ ' key\ '] and $arr [key] We can all show what we want to close, but where do they differ?

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.