manianprasanna Posted September 7, 2006 Share Posted September 7, 2006 I would like to write a looping function. for example as below[code]loop(10) { echo 'Please answer me';}[/code]So that the above code would print 10 times ''Please answer me''. Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code. So could anyone tell me that whether i can write a looping function. If yes then how?Please help me. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/ Share on other sites More sharing options...
Orio Posted September 7, 2006 Share Posted September 7, 2006 You should read this:[url=http://www.w3schools.com/php/php_looping.asp]PHP and looping[/url]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87637 Share on other sites More sharing options...
hostfreak Posted September 7, 2006 Share Posted September 7, 2006 This:[code]<?phpfunction loop($times, $text) {for ($i = 0; $i < $times; $i++) {echo "$text<br />";}}loop(10, "Please answer me");?>[/code]Should work. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87638 Share on other sites More sharing options...
manianprasanna Posted September 7, 2006 Author Share Posted September 7, 2006 Thanks for those who had replied so far, but those are not the answers i want. Because as i already said that i want to looping statment.To explain more clear, see the below code[code]loop(10) { for($i=0; $i<5; $i++) { echo 'please help me'; }}[/code]The same loop function should also be able to use like below[code]loop(10) { if($i==8) { echo 'please help me'; }}[/code]I think now you all can understand more clear. To accomplish this type of function calling how should i declare the loop function. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87648 Share on other sites More sharing options...
hostfreak Posted September 7, 2006 Share Posted September 7, 2006 Where are you getting "function" from? I don't see any where in your code where you define a function. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87654 Share on other sites More sharing options...
manianprasanna Posted September 7, 2006 Author Share Posted September 7, 2006 Yes that's what i would like to know that only., that is how to define the function loop to accomplish the functionality said in my previous post.[quote]Thanks for those who had replied so far, but those are not the answers i want. Because as i already said that i want to looping statment.To explain more clear, see the below code[code]loop(10) { for($i=0; $i<5; $i++) { echo 'please help me'; }}[/code]The same loop function should also be able to use like below[code]loop(10) { if($i==8) { echo 'please help me'; }}[/code]I think now you all can understand more clear. To accomplish this type of function calling how should i declare the loop function.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87658 Share on other sites More sharing options...
hostfreak Posted September 7, 2006 Share Posted September 7, 2006 Can't you see looking at the code I posted how to define a function? I don't see why the code I posted won't work for you?[code]<?phpfunction loop($times, $text) {for ($i = 0; $i < $times; $i++) {echo "$text<br />";}}loop(10, "Please answer me"); //First declare the amount of times you want the text within the parenthesis to be displayed. Then within the parenthesis declare the text you want displayed.?>[/code]Maybe I am totally mis-understanding you, if that is the case, I applogize. Maybe you can be more specific? Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87662 Share on other sites More sharing options...
jsimmons Posted September 7, 2006 Share Posted September 7, 2006 [quote author=manianprasanna link=topic=107212.msg429772#msg429772 date=1157626439]I would like to write a looping function. for example as below[code]loop(10) { echo 'Please answer me';}[/code]So that the above code would print 10 times ''Please answer me''. Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code. So could anyone tell me that whether i can write a looping function. If yes then how?Please help me.[/quote]Here's a looping function to do what you specified:[code]<?phpfunction loop($iterations){for ($i = 1; $i <= $iterations; $i++){echo "Please answer me";}}// usage...loop(10);?>[/code]The output on a web page will look like this:[code]Please answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer me[/code]I'm almost sure that's not what you want, but hey, who am I to judge? I now re-quote you:[quote author=manianprasanna link=topic=107212.msg429772#msg429772 date=1157626439]Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code. [/quote]How are we supposed to devine the special knowledge we need to write your code for you? Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87670 Share on other sites More sharing options...
manianprasanna Posted September 7, 2006 Author Share Posted September 7, 2006 Thanks for all those who replied. I would like to tell one more thing. The statemets inside the looping function is not same in all the case. I can be anything like conditional statements, DB manupulations, etrc....The loop function should take those statements as input and loop it.case 1:[code]loop(10) { //some set of statements.}[/code]case 2:[code]loop(10) { //Not the same set of statements as in case 1.}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87674 Share on other sites More sharing options...
hostfreak Posted September 7, 2006 Share Posted September 7, 2006 With my function, you can change what is looped and how many times, for example:[code]<?phpfunction loop($times, $text) {for ($i = 0; $i < $times; $i++) {echo "$text<br />";}}loop(10, "Please answer me"); //Please answer me - looped 10 timesloop(8, "Hello, how are you"); //Hello, how are you - looped 8 times//You could apply statements like:$test = "8";if ($test == "8") {loop(5, "Test equals 8");}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87679 Share on other sites More sharing options...
jsimmons Posted September 7, 2006 Share Posted September 7, 2006 [quote author=manianprasanna link=topic=107212.msg429814#msg429814 date=1157631990]Thanks for all those who replied. [/quote]You're not a programmer, are ya... :/Hostfreak has given you the answer (twice). Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87689 Share on other sites More sharing options...
manianprasanna Posted September 7, 2006 Author Share Posted September 7, 2006 Again thankyou hostfreak. But you had changed my function calling from loop(10) to loop(10, "Please answer me"). First i don't like to have another parameter (your 2[sup]nd[/sup] parameter). And i would like to execute any statements inside the calling loop function.Example 1:[code]loop(10) { ////MySQL insert statements.}[/code]Example 2:[code]loop(10) { ////Some simple echo statemets.}[/code]Example 2:[code]loop(10) { ////Some file manupulations.}[/code]Sorry if i have hurted any body..Please reply me.[quote author=hostfreak link=topic=107212.msg429820#msg429820 date=1157632212]With my function, you can change what is looped and how many times, for example:[code]<?phpfunction loop($times, $text) {for ($i = 0; $i < $times; $i++) {echo "$text<br />";}}loop(10, "Please answer me"); //Please answer me - looped 10 timesloop(8, "Hello, how are you"); //Hello, how are you - looped 8 times//You could apply statements like:$test = "8";if ($test == "8") {loop(5, "Test equals 8");}?>[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87705 Share on other sites More sharing options...
MaaSTaaR Posted September 7, 2006 Share Posted September 7, 2006 Hmmm , i think you need to hacking PHP code to do something like that , becuase it's a statement and not function .you can do it with PHP by compile it , i mean write function open the php source file and search the "loop" statement in the php source file , if find the statement replace it by real loop statement like "while" or "for" , i mean do something like templates engine (for example Smarty) . Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87712 Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 Can you provide proper examples please. Posting[code]loop(10) { ////MySQL insert statements.}loop(10) { ////Some simple echo statemets.}loop(10) { ////Some file manupulations.}[/code]Doesnt help much.A good example will be to post the code you want to be repeated over and over in your loop function.If you dont want a secound paramter, then prehaps use global:[code]function loop($times){ global $text; for ($i = 0; $i < $times; $i++) { echo "$text<br />"; }}$text = "Please answer me";loop(10); //Please answer me - looped 10 times$text = "Hello, how are you";loop(8); //Hello, how are you - looped 8 times[/code]Also you cannot use a function to do three different things, without having more than 1 parameter. How will the loop function know you want to run a query, or manupulate a file, echo something etc. I cannot see where you are going with this. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87741 Share on other sites More sharing options...
kenrbnsn Posted September 7, 2006 Share Posted September 7, 2006 The OP doesn't want a function, he wants a new statement type.PHP already has looping statements: for, while, do ... while. Why can't one of those be used?Example:[code]<?phpfor($i=0;$i<10;$i++) {//// code block to be executed 10 times//}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87756 Share on other sites More sharing options...
hostfreak Posted September 7, 2006 Share Posted September 7, 2006 [quote author=kenrbnsn link=topic=107212.msg429899#msg429899 date=1157638850]The OP doesn't want a function, she wants a new statement type.PHP already has looping statements: for, while, do ... while. Why can't one of those be used?Example:[code]<?phpfor($i=0;$i<10;$i++) {//// code block to be executed 10 times//}?>[/code]Ken[/quote]She? Lol, his profile would suggest otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87759 Share on other sites More sharing options...
roopurt18 Posted September 7, 2006 Share Posted September 7, 2006 The OP appears to want a general function that loops but has a different body depending on from where it's called.I'm guessing their intended use is:[code]// get DB items$items = mysql_query($sql);// Loop over items and do something$results = Loop($items);// Loop over the results and do something else entirely$new_results = Loop($results);[/code]To the OP, if this is what you want, sorry you can't do that. You'll just have to write a new function for each type of looping functionality you want to provide. Quote Link to comment https://forums.phpfreaks.com/topic/19997-to-write-a-looping-function/#findComment-87842 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.