Welcome to WebmasterWorld Guest from 107.20.54.98

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Grab last variable from a While Loop?

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

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 0


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)

Senior Member from US 

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

joined:Mar 31, 2002
posts:7575
votes: 0


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)

Junior Member

10+ Year Member

joined:Jan 19, 2003
posts:63
votes: 0


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)

New User

10+ Year Member

joined:Jan 5, 2004
posts:38
votes: 0


Hi,
Just put an if statement in the loop

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

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

Senior Member from US 

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

joined:Mar 31, 2002
posts:7575
votes: 0


>> Just put an if statement in the loop

Yup, that should work.

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

Full Member

10+ Year Member

joined:June 28, 2003
posts:292
votes: 0


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