4 ms·
I don't get it. An array is a different beast, and this is a major annoyance that php misses proper arrays. When you define $array[10], you summon into existen
by wazoox 16y ago
I don't get it. An array is a different beast, and this is a major annoyance that php misses proper arrays. When you define $array[10], you summon into existence $array[0] to $array[9]. This is an intended and expected side-effect...
- randallsquared 16y agoWhat you want in PHP would be $array = range(0,10); I guess. I can't think of a time when I wanted to assign directly to a non-sequential number in a vector and have a (potentially huge) amount of space taken up "below" it; wouldn't that typically be a bug?
- wazoox 16y agoOf course this example is contrived: my @array; # $#array : number of @array elements say $#array ; # print -1 $array[10]=3; say $#array; # print 10 But it is quite common to populate an array, then delete some elements in it for instance.
- randallsquared 16y agoShouldn't your third line print zero instead of negative one? Maybe negative one is a special result of an uninitialized array in perl? Hm. Anyway, the actual use case of populating an array and then deleting some elements works fine in PHP; I don't think there's any difficulty with expressing what you want -- it's just going to be somewhat less efficient than it could be. In your example, it's not clear to me what indexes zero through nine point to... null, maybe? In PHP, you'd have to be explicit about what values you wanted in zero through nine by using array_fill(), or live with: for($i=0;$i<=10;$i++) { if(!isset($array[$i])) continue; // ... } If you use a for loop, you'd have to check each value to make sure it's defined like that. If you don't care about order in the loop, you could use foreach and avoid the check and a lot of extraneous looping.