Re: end of array

From: Date: Mon, 24 Jan 2005 05:02:04 +0000
Subject: Re: end of array
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-207109@lists.php.net to get a copy of this message
"M. Sokolewicz" <tularis@php.net> writes: > Raj Shekhar wrote: > > "M. Sokolewicz" <tularis@php.net> writes: > > > >>Raj Shekhar wrote: > > > >>>$n_elts = count($myarray); > >>>for ($i=0; $i< $n_elts ; $i++) > >>>{ > >>> if ($i = $n_elts -1) > > ^^^ > > Use of == required to make it work > >>> { > >>> echo "On last element"; > >>> break; > >>> } > >>> else > >>> { > >>> echo "Somwhere in the middle"; > >>> } > >>>} > >> > >>that's an eternal loop in case you hadn't noticed (*rolls eyes*) > > Oops :( not eternal loop though, only one loop > why one? > for($i=0; $i<$n;$i++) { > $i = ($n-1); > } My statement was, if ($i = $n_elts -1) { echo "On last element"; break; } i.e. - assign ($i = $n_elts -1) and check the return value of the assignment. - If the assignment succeeds (which should, unless you are running short of free memory) THEN - echo - BREAK out of loop Since this conditions are met the first time the loop runs, the loop will run only once. I know I am correct, since I ran the code this time :P -- Raj Shekhar System Administrator, programmer and slacker home : http://rajshekhar.net blog : http://rajshekhar.net/blog/

« previous php.general (#207109) next »