Welcome to WebmasterWorld Guest from 54.158.65.139

Forum Moderators: coopster & jatar k

Grab last variable from a While Loop?

   
4:40 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Hi,

Im currently getting info from a loop like this:

###start###

$numberofchildren = "3";

$current = 0;
while ($current < $numberofchildren) {
++$current;

$a="a";
$b="a";

if ($a=="$b") {
echo "test$current ";
}
}

###end###

This will Print: "test1 test2 test3"

How can I get it to ONLY print "test3"?

Any help is appreciated.

4:59 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Um... why are you using a loop if that's all you want to do? There must be some parameters other than what you've given us and they'll most likely influence our answers. ;)
5:03 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



The actual code is much more complicated. This may help, the $numberofchildren variable can vary, it can be anything from 1 - 12.

The code is a sample that actually works.

So how can I just get back the last variable info?

thanks!

5:15 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Hi,
Just put an if statement in the loop

if ($current == $noofchildren) then do something

5:58 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



>> Just put an if statement in the loop

Yup, that should work.

7:45 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Or you could just do the printing outside the while loop--it will naturally have the last value of the loop.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month