PHP array questions about numeric key names

Source: Internet
Author: User
Tags numeric

The following is a summary of the PHP array numeric key names:

The length of the key can only be within the length of the int, and will appear overwritten after the int range.

When you access a value in the range of a key name of int, PHP forces the numeric key name to be converted to the INT value type

When the number key name is longer than 19 digits, it becomes 0

The string or numeric type is the same when the key name is normal length

$i = 126545165;

$arr [' 126545165 '] = ' abc ';

$arr [126545165] = ' UiO ';

Var_dump ($arr);

Echo ';

Var_dump (Isset ($arr [$i]));

When the length exceeds the integer, the key name is confused < yo http://www.2cto.com/kf/ware/vc/"target=" _blank "class=" Keylink ">vcd4kpha+phbyzsbjbgfzcz0=" Brush:java; " > $i = 12312312312312; $arr [' 1000000000147483649 '] = ' abc '; $arr [1000000000147483649] = ' UiO '; Var_dump ($arr); Echo '

'; Var_dump (Isset ($arr [$i]));

When the length exceeds 20 digits, the key name becomes 0

$i = 123123123123123123123123123123;

Var_dump ($i);

Echo ';

$arr [123123123123123123123123123123] = ' abc ';

$arr [Strval (123123123123123123123123123123)] = ' abc ';

Var_dump ($arr);

Echo ';

Var_dump (Isset ($arr [$i]));

Echo ';

Var_dump (Isset ($arr [Strval ($i)]));

Echo ';

Var_dump (Array_keys ($arr));

The variable is accessed directly as a key name, and the result is different

$i = 123123123123123;

Var_dump ($i);

Echo ';

$arr [$i] = ' abc ';

$arr [Strval ($i)] = ' abc ';

Var_dump ($arr);

Echo ';

Var_dump (Isset ($arr [$i]));

Echo ';

Var_dump (Isset ($arr [Strval ($i)]));

Echo ';

Var_dump (Array_keys ($arr));

From several of the above tests, see:

If the key name is a number and the range is within an int, the string or int does not affect access

If the length is greater than int, it is automatically converted to float, the conversion for access occurs in all sorts of confusion, even directly into 0, so it is best to convert to string type

$i = 123123123123123123123123123123;

$j = ' 123123123123123123123123123123 ';

$arr 1[strval ($i)] = ' abc ';

$arr 2[$j] = ' abc ';

Var_dump ($arr 1);

Echo ';

Var_dump ($arr 2);

So in the dynamic operation of the PHP array, if it is not possible to determine whether the key name will appear number or length is greater than int, then unify the key name Strval into a string to operate the most secure

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.