Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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];
?>

Output

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

B

B

A

B

Q1. Why is $test[0] same as $test["0"] whereas $test[00] is not same as $test["00"]

Q2. If the answer to Q1 is that because 00 = 0 numerically then why does this array have one index as 00 and another as 0?

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

Edit

Based on the answers so far, there is another question in my mind. Here goes Question 4.

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

Q5.

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

Why the output is B, should it not be A ? because that is the first element in the array, at 0th position

share|improve this question

3 Answers

up vote 5 down vote accepted

According to the documentation, one of "the […] key casts [that] will occur" is:

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.

[link]

share|improve this answer
 
But how can they declare 08 not to be same as 8 in mathematics? 08 should be a valid numeric value of 8 and why is a 00 equal to 0 but a 08 not equal to 8 –  Hanky 웃 Panky May 30 at 5:57
 
@CORRUPT not in this case –  Hanky 웃 Panky May 30 at 5:59
 
this piece of doc says only about casting array indexes and they need to be "valid decimal integer". "0" is 0 but "00" is a string, 00 is an integer 0 so it wont be casted. your example says it all. –  fsw May 30 at 6:12
2  
so answer to Q4 is (07==7) is true! but "07"!=7 when it comes to array indexes –  fsw May 30 at 6:14
 
@fsw if your argument was to be taken that 00 is a string then why does PHP evaluate this to true ? if(7=="07") –  Hanky 웃 Panky May 30 at 6:21
show 9 more comments

Q1. Because 00 = 0 numerically

Q2. Because the index is "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

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.

Edited after comment

Q4. I think OP's question is essentially why the second and the forth 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.

share|improve this answer
 
What is the difference between 00 and "00" if the difference between 0 and "0" is nothing? –  Hanky 웃 Panky May 30 at 5:47
 
But how can they declare 08 not to be same as 8 in mathematics? 08 should be a valid numeric value of 8 and why is a 00 equal to 0 but a 08 not equal to 8 –  Hanky 웃 Panky May 30 at 5:59
2  
Numbers starting with 0 are octal numbers, so 010 == 8. 08 is an invalid octal number and is interpreted as 0, so var_dump(08 == 0) prints bool(true). –  yaz Jun 8 at 8:26
"anystring" == 0; //true
"0000" == 0;      //true

"0" == 0;      //true
share|improve this answer
 
This is false. Any string not starting with a number does evaluate to zero. In other words, your third statement is false. ("1" == 0); //false –  yaz Jun 8 at 8:28
 
OK i del it ,thk –  JOE LEE Jul 16 at 5:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.