PHP Array Question

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

PHP Array Question

Post 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.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: PHP Array Question

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Array Question

Post by social_experiment »

Looks interesting
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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Array Question

Post 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? :)
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: PHP Array Question

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Array Question

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Array Question

Post 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)
(#10850)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Array Question

Post by Jonah Bron »

Wait, you had to manually calculate the location of an array element in Fortran? Tough. I thought that was only Assembly.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Array Question

Post by Christopher »

No ... Fortran and C both made arrays nicer than Assembly. But they used zero based indexes to make the pointer math simple.
(#10850)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP Array Question

Post by McInfo »

Ajaxdevelopment wrote:we can also access the array elements as'i[a]' instead of 'a'

It works in C, but not PHP.
beetree
Forum Commoner
Posts: 26
Joined: Mon Jul 18, 2011 6:30 pm
Location: Peninsula

Re: PHP Array Question

Post 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
Post Reply