On Tue, 20 Feb 2001 15:10, Keith Devens wrote:
> Hi, I'm getting some weird behavior from prev() and next(). If anyone
> knows what's going on please let me know. Also, please make sure you
> e-mail me personally in addition to the list.
>
> Anyway, here's the problem. Check out the following code:
>
> <?php
> $arr = Array(1, 2, 3, 4);
>
> echo current($arr) . "<br>"; #prints out 1
> echo next($arr) . "<br>"; #prints out 2
> echo next($arr) . "<br>"; #prints out 3
> echo next($arr) . "<br>"; #prints out 4
> echo prev($arr) . "<br>"; #prints out 3
> echo prev($arr) . "<br>"; #prints out 2
> echo prev($arr) . "<br>"; #prints out 1
> ?>
>
> It does the expected and prints out 1234321 (without the <br>s).
> However, if I add one more call to next() in there, it only prints out
> 1234. It seems like the call to next that goes past the end of the
> array sort of "breaks" it so you can't go back again. If I do a call to
> end() it prints out 4, so the array still 'works', but I can't get back
> using prev(). Anyone know why? Thanks!
>
> Keith

Ha - finally a use for all that stuff I learned about pointers in 
Advanced Programming :-) (And it's in the docs if you look)

According to the docs, "If advancing the internal array pointer results 
in going beyond the end of the element list, next() returns false" so 
when you try an print out the result, you get nothing. And once the 
pointer goes beyond the end (or beginning, with prev) its position is 
undefined and you need to use an absolute positioning instruction like 
end or reset to relocate the pointer in a known position.

Analogy - it's a bit like walking up and down stairs - if you are 
currently on a step then 'go up one step' or 'go down one step' makes 
sense; but if you step off the stairs then you could be anywhere in 
relation to the stairs, even out side the building.

Hmm, that's not a real good analogy - someone else?


-- 
David Robley                        | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES  | http://www.nisu.flinders.edu.au/
AusEinet                            | http://auseinet.flinders.edu.au/
            Flinders University, ADELAIDE, SOUTH AUSTRALIA

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to