ngng Posted November 23, 2007 Share Posted November 23, 2007 I just want to know when I reach the final loop. Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 Not exactly know your purpose though while($k=0;$k<=$limit;$k++) { if($k=$limit-1)?echo "LAST LOOP"; } Quote Link to comment Share on other sites More sharing options...
Barand Posted November 23, 2007 Share Posted November 23, 2007 As wsantos points out, it's easy with a for loop as the number of iterations is known in advance, but not so straightforward with a while loop. Depend on what you are doing. If, say, you are composing a list of comma-separated items but don't want the comma after the final one, then you can do it by output the comma before the items but NOT before the first. You easily know when you are on the first. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 23, 2007 Share Posted November 23, 2007 Personally I would simply remove the last comma after the whole string was created - that way you are not evaluating a conditional in each loop - should be a bit faster... Quote Link to comment Share on other sites More sharing options...
Barand Posted November 23, 2007 Share Posted November 23, 2007 My point was that you can often change the logic so a different action is taken on a known first item rather than an unknown last item. In the absence of info about what s/he is doing I just used that as an example. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 23, 2007 Share Posted November 23, 2007 Sorry Barand - I was doing my 'read just some of the topic' trick. Ergo I decided that the OP was asking about string generation.... Wasn't dissin your kung foo dude... Quote Link to comment Share on other sites More sharing options...
helraizer Posted November 23, 2007 Share Posted November 23, 2007 I just want to know when I reach the final loop. You can get each part of your loop to dump a message; that way you can tell where you are in the loop: echo "this part of the loop is being executed"; --or-- die("this part of the loop is being executed"); die is merely an unformatted version of echo.. with echo you could write <?php echo "<div style='background-color:#000000; color:#FFCD32;'>"; echo "this part of the loop is being executed"; echo "</div>"; ?> where as die(); would be just a message on the screen. So you can then tell which stage of the loop you are at. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 23, 2007 Share Posted November 23, 2007 or you can test for the exit condition <?php $k = 0; while ( $k != 5) { $k = rand(1,9); echo $k ; if ($k==5) echo " Last"; echo '<br>'; } ?> Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 23, 2007 Share Posted November 23, 2007 Well its a classic case of a blurry context of the problem...LOL I even made a typo... while($k=0;$k<=$limit;$k++) { if($k=$limit-1)?echo "LAST LOOP"; } Versus for($k=0;$k<$limit;$k++) { if($k=$limit-1)?echo "LAST LOOP"; } or you can test for the exit condition <?php $k = 0; while ( $k != 5) { $k = rand(1,9); echo $k ; if ($k==5) echo " Last"; echo '<br>'; } ?> In this situation it will work however technically the conditional statement inside the loop will not be reached if the while statement is iterative. LOL...What is your exact problem ngng? Quote Link to comment Share on other sites More sharing options...
ghe Posted November 26, 2007 Share Posted November 26, 2007 Good Day, Ahm, in my opinion, the conditional statement inside the while loop of barrand will still be reached, since $k would only get its value from the rand() function inside the loop. Regards, Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 26, 2007 Share Posted November 26, 2007 In that situation yes...however I did mention that the if the loop is iterative in nature it will never be reached ... meaning a continuous function rather than the discrete values generated by rand Quote Link to comment 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.