$ Array [0] and $ array ['0 ']

Source: Internet
Author: User
The difference between $ array [0] and $ array [0] is as follows: Title $ array0 = $ array [0] and $ array0 = $ array ['0'] both get the first of the array difference between values


Reply to discussion (solution)

Generally, no single quotes are required.

Because they are all numbers 0, there is no difference

Writing a program is still well written by general rules, so that code specifications are not prone to errors and are easy for others to read.

Because they are all numbers 0, there is no difference



What if I change to 1 2 3 ??

Writing a program is still well written by general rules, so that code specifications are not prone to errors and are easy for others to read.


Well, there's no difference between them.

In case of minor differences in execution, $ array [0] calls the strtol function of C less than once (actually: __strtol_internal function)

Is there no difference? Which of the following is a string? Non-int

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

$ Array [0]-> bar1
$ Array ['0']-> bar2

Is there no difference? Which of the following is a string? Non-int

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

$ Array [0]-> bar1
$ Array ['0']-> bar2



Agree with the upstairs comments
In the brackets, the key value can be an integer or string type '', indicating the string type. If no value is added, the key value is considered as an integer 0.

As long as it is a number, there is no difference between the value type and the character type.

var_dump(3.2 == '3.2'); //true




Because they are all numbers 0, there is no difference



What if I change to 1 2 3 ??

The data types of key names are different, and the running speed can be improved by adding single quotes.


Is there no difference? Which of the following is a string? Non-int

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

$ Array [0]-> bar1
$ Array ['0']-> bar2



Agree with the upstairs comments
In the brackets, the key value can be an integer or string type '', indicating the string type. If no value is added, the key value is considered as an integer 0.




In the PHP array, if the index string can be converted to a number, it will also be converted to a digital index. a step-by-step conversion is required.

Is there no difference? Which of the following is a string? Non-int

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

$ Array [0]-> bar1
$ Array ['0']-> bar2


I tried to knock it down, and all output results were bar2, therefore, the idea above is that "if the index string in the PHP array can be converted to a number, it will also be converted to a digital index. there is a step-by-step conversion process"

? Word ???.

However
$ Array [a] in combination with $ array ['A ??, The former? Movie? Efficiency.

As long as it is a number, there is no difference between the value type and the character type.

var_dump(3.2 == '3.2'); //true





Because they are all numbers 0, there is no difference



What if I change to 1 2 3 ??



The return value is True because the data type is automatically converted for comparison. if you use = for comparison, the return value is false.

Php is a weak type language and is automatically converted to a suitable data type during computation.
I did the following test and came to the conclusion:
The quoted integer string is automatically converted to a number as an integer index. non-integer numbers are not converted and are still treated as string indexes.
A number without quotation marks. if it is not an integer, it is rounded up and used as an integer index.

In addition, a string that is not defined as a constant is used as an index. If no quotation marks are provided, a warning error may occur even if the correct result is obtained.

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.