Ok, but if it would be that way I shouldn't get '122334455' for second output, no? The item count increments with every iteration of the loop. Or you're saying that, it checks for an existance of nextitem before every loop, and that will fail with just one element, but will always return true with two elements? ( since the first elements copy is pushed as third element, etc ) -----Original Message----- From: Robert Cummings [mailto:robert@xxxxxxxxxxxxx] Sent: Tuesday, July 05, 2011 4:28 PM To: Dajka Tamás Cc: php-general@xxxxxxxxxxxxx Subject: Re: Foreach question On 11-07-05 10:20 AM, Dajka Tamás wrote: > Hi, > > Yeah, I'm really want to do that, since I'm working with the elements of the original array ( skipped that part in sample code ). > > I've tried your suggestion, but it gives the same result, so on just one input is just gives back '1'. Ahhh... you want the behaviour of the multiple elements... I presumed you wanted the other way around. > What troubles me, that foreach gives an inconsistent working. Why is 'foreach' checking element count at all and working differently with different element counts? That's not normal is my opinion. 'foreach' shouldn't do this: > > if ( count($elements) == 1 ) then loop 1; > else loop normally; > > and that's what is does now, since when it's more than one element it's working like a while loop, with checking the condition before ( and after ) every run. ( if 'foreach' would check that the current run is the last one before executing the current loop, the results would be the same with each case ) You're making an assumption that it is checking the count. It may just be pre-determining whether another element exists for the next iteration. Consider the following pseudo code: nextItem = items->reset(); while( nextItem ) { item = nextItem; nextItem = items->next(); // Do stuff. } There's lots of ways to program a loop... and your PHP foreach loop is being converted to something entirely different internally. The above doesn't count elements, but it will result in the same behaviour as you are experiencing. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php