homepage Welcome to WebmasterWorld Guest from 54.167.179.48
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Grab last variable from a While Loop?
Schoolbag

10+ Year Member



 
Msg#: 3061 posted 4:40 pm on Mar 3, 2004 (gmt 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.

 

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3061 posted 4:59 pm on Mar 3, 2004 (gmt 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. ;)

Schoolbag

10+ Year Member



 
Msg#: 3061 posted 5:03 pm on Mar 3, 2004 (gmt 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!

webdevjim

10+ Year Member



 
Msg#: 3061 posted 5:15 pm on Mar 3, 2004 (gmt 0)

Hi,
Just put an if statement in the loop

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

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3061 posted 5:58 pm on Mar 3, 2004 (gmt 0)

>> Just put an if statement in the loop

Yup, that should work.

jonknee

10+ Year Member



 
Msg#: 3061 posted 7:45 pm on Mar 3, 2004 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved