Page 1 of 1
PHP Array Question
Posted: Wed Apr 06, 2011 4:50 pm
by lenton
I was just wondering..
Why do arrays start at 0 and not 1?
I nearly always find this a problem and have to -1 or +1.
Re: PHP Array Question
Posted: Wed Apr 06, 2011 11:04 pm
by greyhoundcode
lenton wrote:Why do arrays start at 0 and not 1?
I would suppose that if you didn't use zero then that diminishes the range of possible indices by 1.
lenton wrote:I nearly always find this a problem and have to -1 or +1.
In what context?
Re: PHP Array Question
Posted: Thu Apr 07, 2011 11:40 am
by social_experiment
Looks interesting
This might give some insight, although it's not focused on php
Re: PHP Array Question
Posted: Thu Apr 07, 2011 5:40 pm
by Jonah Bron
The first item in the array is at position "zero". Think of that number as the distance between that array item, and the beginning of the array. Arrays start with zero for the same reason that rulers/tape measures do: zero is the beginning, not one.
greyhoundcode wrote:I would suppose that if you didn't use zero then that diminishes the range of possible indices by 1.
That sounds like a joke?

Re: PHP Array Question
Posted: Fri Apr 08, 2011 9:34 am
by greyhoundcode
greyhoundcode wrote:I would suppose that if you didn't use zero then that diminishes the range of possible indices by 1.
Jonah Bron wrote:That sounds like a joke?

Well, not really, but I didn't articulate it very well - the link social_experiment posted does a better job.
Google Groups - Programmers Avenue wrote:Why can't we start at one then? If we started at one, then you'd be doing this:
Code: boolean array[5] OR boolean *array = *(array+sizeOf(boolean)*5)
Memory: 0 0 0 0 0
Code: array[1] = true, array[2] = true, array[3] = true, array[4] = true;
Memory: 0 1 1 1 1
You'd be leaving one bit unused (assuming that booleans use one bit - they might not, but for argument's sake they do now).
One bit. Big deal. What about a struct? Or an array of arrays? Or an array of arrays of arrays? You're going to waste a whole lot of memory.
Re: PHP Array Question
Posted: Fri Apr 08, 2011 5:49 pm
by McInfo
I like Jonah's ruler analogy. 0 is where the first unit starts (its address). 1 is where the first unit stops and the second unit begins, and so on. Memory is read in one direction (forward), so if you are at address 1, you are past the first unit and it is too late to read it.
Re: PHP Array Question
Posted: Fri Apr 08, 2011 6:01 pm
by Christopher
Yeah ... the reason that arrays start with zero goes back to mathematics and intervals. In languages like Fortran and C, arrays had fixed size elements -- so you could easily calculate the memory address of an array element with:
address = base_address + (index * element_size)
Re: PHP Array Question
Posted: Sat Apr 09, 2011 8:47 pm
by Jonah Bron
Wait, you had to manually calculate the location of an array element in Fortran? Tough. I thought that was only Assembly.
Re: PHP Array Question
Posted: Sat Apr 09, 2011 9:08 pm
by Christopher
No ... Fortran and C both made arrays nicer than Assembly. But they used zero based indexes to make the pointer math simple.
Re: PHP Array Question
Posted: Mon Jul 18, 2011 12:41 pm
by McInfo
Ajaxdevelopment wrote:we can also access the array elements as'i[a]' instead of 'a'
It works in C, but not PHP.
Re: PHP Array Question
Posted: Mon Jul 18, 2011 7:13 pm
by beetree
The usage of 0 instead of 1 steams from the old ASM or C times. Back then you had a pointer e.g. to a string (char *str;). str would only be a pointer to an address in the memory and to get the value of the first character in the string you would use *str. To get the second value of the string you would have to enter *(str+1) to first increase the pointer to point to the +1 address in the memory and fetch you the second element of the string.
Hope this helps!
/beetree