"A","B","C","D","E"); print_r($test); echo "
"; echo...">

Are PHP Integer indexes the most numeric?

Consider this example

<?php $test = array("00"=>"A","B","C","D","E"); print_r($test); echo "<br>"; echo $test[0]; echo "<br>"; echo $test["0"]; echo "<br>"; echo $test["00"]; echo "<br>"; echo $test[00]; ?> 

Exit

Array ([00] => A [0] => B [1] => C [2] => D [3] => E)

IN

IN

BUT

IN

Q1. Why is $test[0] the same as $test["0"] , while $test[00] not the same as $test["00"]

Q2. If the answer to Q1 is that since 00 = 0 numerical, why does this array have one index as 00 and the other as 0 ?

Q3. If you cannot access $test["00"] using $test[0] , how do you know which index is numeric and which is a string? if both are just numbers

Edit

Based on the answers, there is still another question in my mind. Here comes question 4.

Q4. Why is if(00==0) true and if(07==7) false? (for array indices)

Q5.

 $test = array("00"=>"A","0"=>"B","000"=>"C","0000"=>"D","00000"=>"E"); echo $test[0]; 

Why conclusion B if it is not A ? because this is the first element in the array, at position 0

+8
arrays php numbers indexing
source share
3 answers

According to the documentation, one of the "[hellip;] keys will produce [,]]:

A string containing a valid integer will be passed to an integer . For example. the key β€œ8” will actually be stored under 8. On the other hand, β€œ08” will not be selected, since it is not a valid decimal integer.

[ link ]

+5
source share

Q1. Since 00 = 0 is numerically

Q2. Since the index "00" not 00

Try:

 $test=array(00=>"A","B","C","D","E"); print_r($test); /* Array ( [0] => A [1] => B [2] => C [3] => D [4] => E ) */ 

Q3.

 echo gettype("00"); # string echo gettype(00); # integer echo gettype("0"); # string echo gettype(0); # integer 

From the manual: http://php.net/manual/en/language.types.array.php

Lines containing real integers will be passed to an integer type. For example. the key β€œ8” will actually be stored under 8. On the other hand, β€œ08” will not be selected, since it is not a valid decimal integer.

Edited after comment

Q4. I think the OP question is, in fact, why the second and fourth behave differently:

 php > var_dump("00" === 0); bool(false) php > var_dump(00 === 0); bool(true) php > var_dump("08" === 8); bool(false) php > var_dump(08 === 8); bool(false) 

I have no answer yet.

+2
source share
 "anystring" == 0; //true "0000" == 0; //true "0" == 0; //true 
-one
source share

All Articles