PHP Array Question
Moderator: General Moderators
PHP Array Question
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.
Why do arrays start at 0 and not 1?
I nearly always find this a problem and have to -1 or +1.
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: PHP Array Question
I would suppose that if you didn't use zero then that diminishes the range of possible indices by 1.lenton wrote:Why do arrays start at 0 and not 1?
In what context?lenton wrote:I nearly always find this a problem and have to -1 or +1.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP Array Question
Looks interesting
This might give some insight, although it's not focused on php
This might give some insight, although it's not focused on php
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP Array Question
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.

That sounds like a joke?greyhoundcode wrote:I would suppose that if you didn't use zero then that diminishes the range of possible indices by 1.
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: PHP Array Question
greyhoundcode wrote:I would suppose that if you didn't use zero then that diminishes the range of possible indices by 1.
Well, not really, but I didn't articulate it very well - the link social_experiment posted does a better job.Jonah Bron wrote:That sounds like a joke?
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
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP Array Question
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)
address = base_address + (index * element_size)
(#10850)
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP Array Question
Wait, you had to manually calculate the location of an array element in Fortran? Tough. I thought that was only Assembly.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP Array Question
No ... Fortran and C both made arrays nicer than Assembly. But they used zero based indexes to make the pointer math simple.
(#10850)
Re: PHP Array Question
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
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
Hope this helps!
/beetree