ag3nt42 Posted June 9, 2008 Share Posted June 9, 2008 I'm building a nice big web app and I'm starting to run low of letters to use for variables and looping needs.. I know i can create local variables but I can't seem to get them to work so i'm assuming i'm not using them correctly thats what i need some help with.. I want to do this... while($i=0;$i<=15) { DO THIS BALH } while($i=0;$i<=100) { blah blah do this } instead of... $i=0; $y=0; while($i<=15) { blah do this } while($y<=100) { blah do this and htis } any suggestions?? thanx alot everyone, ag3nt42 Quote Link to comment https://forums.phpfreaks.com/topic/109438-local-variable-declaration-while-statements/ Share on other sites More sharing options...
craygo Posted June 9, 2008 Share Posted June 9, 2008 as long as you don't need the variable further down in your script. You can just keep using the same one over and over again. PHP will just keep re-assigning it. The only time you have to redeclare it is when you need that value later in the script. Ray Quote Link to comment https://forums.phpfreaks.com/topic/109438-local-variable-declaration-while-statements/#findComment-561340 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 Are you wanting to do a for loop instead of a while loop? for($x = 0; $x < 10; $x++) { // do something here } Quote Link to comment https://forums.phpfreaks.com/topic/109438-local-variable-declaration-while-statements/#findComment-561353 Share on other sites More sharing options...
ag3nt42 Posted June 9, 2008 Author Share Posted June 9, 2008 i guess i do want a for loop... what really is the difference on the looping? it seems to me that while looping an for looping is relatively the exact same thing aside from the syntax Quote Link to comment https://forums.phpfreaks.com/topic/109438-local-variable-declaration-while-statements/#findComment-561361 Share on other sites More sharing options...
.josh Posted June 9, 2008 Share Posted June 9, 2008 With a for loop, you are executing a specific amount of iterations. With a while loop, you are executing it indefinitely until the condition is false. The usefulness of a while loop is for instance when you are retrieving info from a database and you don't necessarily know how many results are going to be returned, so you don't really know how many iterations to make the loop. Quote Link to comment https://forums.phpfreaks.com/topic/109438-local-variable-declaration-while-statements/#findComment-561368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.