The difference between $array [0] and $array[' 0 ']

Source: Internet
Author: User
such as the title $array 0= $array [0] and $array0= $array [' 0 '] can get the first value of the array to distinguish


Reply to discussion (solution)

In general, single quotes are not required.

Because they're all numbers 0, so there's no difference.

Writing programs or general rules is good, so code specifications, not prone to error, but also convenient for other people to read.

Because they're all numbers 0, so there's no difference.



What if we change to 1 2 3?

Writing programs or general rules is good, so code specifications, not prone to error, but also convenient for other people to read.


Now, that means there's no difference between these two.

Fine-grained execution when $array [0] Less call C strtol function (in fact: __strtol_internal function)

No difference? Plus single quotes to represent a string? non-int

$array = Array ("1" = "bar1", "0" = "bar2")

$array [0], BAR1
$array [' 0 ']-bar2

No difference? Plus single quotes to represent a string? non-int

$array = Array ("1" = "bar1", "0" = "bar2")

$array [0], BAR1
$array [' 0 ']-bar2



Agree with the upstairs view
In parentheses, the key value can be an integer and the string type ' indicates a type of string, if not added to the integer 0

As long as it is a number, there is no difference between a numeric type and a character type

Var_dump (3.2 = = ' 3.2 '); True




Because they're all numbers 0, so there's no difference.



What if we change to 1 2 3?

The data type of the key name is different, plus the single quote run speed can improve


No difference? Plus single quotes to represent a string? non-int

$array = Array ("1" = "bar1", "0" = "bar2")

$array [0], BAR1
$array [' 0 ']-bar2



Agree with the upstairs view
In parentheses, the key value can be an integer and the string type ' indicates a type of string, if not added to the integer 0




In the PHP array, if the index string can be converted to a number, it will be converted to a digital index. The Step conversion process

No difference? Plus single quotes to represent a string? non-int

$array = Array ("1" = "bar1", "0" = "bar2")

$array [0], BAR1
$array [' 0 ']-bar2


Try to knock, the results are bar2, so it is the view upstairs "in the PHP array, if the index string can be converted to a number, it will be converted to a digital index more than the step conversion process."

Word???。

But
$array [A] Hopewell $array [' a '] have??, the former shadow efficiency.

As long as it is a number, there is no difference between a numeric type and a character type

Var_dump (3.2 = = ' 3.2 '); True





Because they're all numbers 0, so there's no difference.



What if we change to 1 2 3?



The return true is because the automatic conversion data type to compare, if you replace with = = = To compare, return is false.

PHP is a weakly typed language that is automatically converted to the appropriate data type at the time of operation.
Just made the next test, and came to the conclusion that:
Quoted integer strings are automatically converted to numbers, indexed as integers, non-integer numbers are not converted, and are still treated as string indexes.
unquoted numbers, non-integers are rounded and then indexed as integers

In addition, a string that is undefined as a constant is indexed, without the quotation marks, although the correct result can be obtained, but there is a warning error.
  • 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.