Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there any difference between integer index and string index of PHP arrays (except of course the fact that the latter is called associative array)?

So for example, what are the differences between the following two arrays:

$intIndex[5] = "Hello";
$intIndex[6] = "World";
$intIndex[7] = "!";

And

$strIndex['5'] = "Hello";
$strIndex['6'] = "World";
$strIndex['7'] = "!";

In the first case, what happens to $intIndex[0] to $intIndex[4]?

share
    
It gives you an Undefined index 0/4 PHP warning. –  TiMESPLiNTER 2 mins ago
    
@TiMESPLiNTER Could you please clarify why it gives that error? I thought in PHP, there is no need for variable declaration. –  lonekingc4 2 mins ago
    
Well you don't have an entry in your array with key 0 or 4. It's an undefined index in this array. So if you use $intIndex[0] you will run into an Undefined index PHP warning. –  TiMESPLiNTER 1 min ago
    
@TiMESPLiNTER Okay, so is that memory unallocated? I mean, even if I use $arr[1000] directly, the first 999 indices are not wasted? –  lonekingc4 7 secs ago

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.