Lumana Posted June 25, 2021 Share Posted June 25, 2021 suppose i want to loop number like this (1.2.3.4.5.....100), but after every 5 number i want to echo another word and after that continue to the first loop: example: 1.2.3.4.5.W.6.7.8.9.10.W.11.12.13.14.15W.... can anyone help me about this. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/ Share on other sites More sharing options...
requinix Posted June 25, 2021 Share Posted June 25, 2021 What code have you tried so far, and what kind of problem (if any) are you having with it? Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587551 Share on other sites More sharing options...
Lumana Posted June 26, 2021 Author Share Posted June 26, 2021 I tried this and come out like this, I think my problem is solved though its not very precise way, It can be write in more better way. <!DOCTYPE html> <html> <body> <?php for ($x = 0; $x <= 42; $x++) { if($x == "5" || $x == "10" || $x == "15" || $x == "20" || $x == "25" || $x == "30" || $x == "35" || $x == "40" || $x == "45") { echo "<b>I love you</b> <br>"; } echo "The number is: $x <br>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587565 Share on other sites More sharing options...
requinix Posted June 26, 2021 Share Posted June 26, 2021 I'm sure you noticed, but you're counting from 0 to 42. Should be easy to change to count from 1 to 100, right? So you can list out all the numbers you want to output a message after, but you have to list them all out. What if you needed a loop from 1 to 1000? Or a million? What would you do then? You want to output at 5, 10, 15, etc. In other words, numbers divisible by 5. Have you tried looking for a solution using something that can tell you if a number is divisible by 5? 1 Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587566 Share on other sites More sharing options...
jodunno Posted June 26, 2021 Share Posted June 26, 2021 Hi Lumana, You could reduce the comparison branching to a single comparison with a count . <?php $Count = 5; for ($j = 1; $j < 101; $j++) { if ($j == $Count) { echo $j . ' Boogie Woogies' . '<br>'; $Count += 5; continue; } echo $j . '<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587567 Share on other sites More sharing options...
Barand Posted June 26, 2021 Share Posted June 26, 2021 The modulo operator was invented for situations like this $mytext = "I ❤ PHP"; for ($i=1; $i<=100; $i++) { echo "$i<br>"; if ($i % 5 == 0) echo "$mytext<br>"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587569 Share on other sites More sharing options...
jodunno Posted June 26, 2021 Share Posted June 26, 2021 hmm. I didn't think about modulus so it is a better example of reducing the code but your definition of it is not correct. Modulus is simply remainder and calculating using remainder (hence, ==0 means no remainder.) I learned that years ago when i encountered modulus in a Microsoft QBasic Gorilla game. I never heard of it before since my Math teachers only used the word remainder. Then again, I never had a proper algebra or calculus education. I had to teach myself (using teach yourself books). Good contribution, Barand. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587571 Share on other sites More sharing options...
Barand Posted June 26, 2021 Share Posted June 26, 2021 6 minutes ago, jodunno said: but your definition of it is not correct. In what respect is it not correct? I didn't define it. https://www.php.net/manual/en/language.operators.arithmetic.php Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587572 Share on other sites More sharing options...
jodunno Posted June 26, 2021 Share Posted June 26, 2021 "The modulo operator was invented for situations like this" it was invented? it was invented to handle division? it was invented to handle division by 5? I'm pretty sure that remainders are as old as division. The term is simply a word to refer to them and the concept is a way to work with them. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587574 Share on other sites More sharing options...
Barand Posted June 26, 2021 Share Posted June 26, 2021 4 minutes ago, jodunno said: I'm pretty sure that remainders are as old as division. You are correct. Are you also saying that the mathematicians from those ancient civilizations also came up with the "PHP modulo operator"? That would be excellent foresight. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587575 Share on other sites More sharing options...
jodunno Posted June 26, 2021 Share Posted June 26, 2021 okay, i see. you are the big php gorilla in the room and you want to pound your chest when someone is out of place challenging you. i didn't expect this behaviour from you. You are always right. You are omniscient and i should bow down to you. Fine, i kow tow your excellency. You are so much smarter than me. I'm have no iq. I no nothing about programming. Only Barand knows. I will add to my notes: modulus is defined in php with an illogical % symbol (per centum, 100). modulus only exists because of php. So when you see a ≡ b (mod n) it was invented by php developers. gotcha. I will be sure to find my place in stuporville. I know nothing so i should not contribute to this forum. I should only ask questions from the Wizard of Oz but never look behind the curtain. I will stay away from this forum and let you pound your chest. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587576 Share on other sites More sharing options...
Barand Posted June 26, 2021 Share Posted June 26, 2021 OK. You can always come back when you have finished throwing your toys out of your pram. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587581 Share on other sites More sharing options...
Lumana Posted June 26, 2021 Author Share Posted June 26, 2021 15 hours ago, requinix said: I'm sure you noticed, but you're counting from 0 to 42. Should be easy to change to count from 1 to 100, right? So you can list out all the numbers you want to output a message after, but you have to list them all out. What if you needed a loop from 1 to 1000? Or a million? What would you do then? You want to output at 5, 10, 15, etc. In other words, numbers divisible by 5. Have you tried looking for a solution using something that can tell you if a number is divisible by 5? Thank you so much sir. your last line open my brain to think , though other people already gave me solution but you really teach me very nicely. My whole life study in business studies, thats the reason i am very much weak in programming. Anyway thank you so much sir. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587599 Share on other sites More sharing options...
Lumana Posted June 26, 2021 Author Share Posted June 26, 2021 12 hours ago, jodunno said: Hi Lumana, You could reduce the comparison branching to a single comparison with a count . <?php $Count = 5; for ($j = 1; $j < 101; $j++) { if ($j == $Count) { echo $j . ' Boogie Woogies' . '<br>'; $Count += 5; continue; } echo $j . '<br>'; } ?> Thank you so much sir... your code is working well and easily understandable also. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587600 Share on other sites More sharing options...
Lumana Posted June 26, 2021 Author Share Posted June 26, 2021 12 hours ago, Barand said: The modulo operator was invented for situations like this $mytext = "I ❤ PHP"; for ($i=1; $i<=100; $i++) { echo "$i<br>"; if ($i % 5 == 0) echo "$mytext<br>"; } Thank you so much sir... your code is working well and easily understandable also. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587601 Share on other sites More sharing options...
ChenXiu Posted June 27, 2021 Share Posted June 27, 2021 On 6/26/2021 at 3:25 AM, jodunno said: okay, i see. you are the big php gorilla in the room Dear Mr./Mrs. Jodunno, just a kind word of advice, if I may. This PHP forum has a small handful of VERY top-level experts. Barand is one of those experts. In my opinion, it would behoove you to not display the rudeness you displayed, especially to Barand. Be disciplined, and hold this type of words to yourself. On a positive note, you did say something 100% correct: the part where you said "...someone is out of place challenging you." Yes, you are definitely out of place. Because you did not even correctly 'challenge' Barand. He did NOT say "modulus was invented".... what he said was the modulo operator was invented for situations like this. And it was. And Barand is right. Again, these are just kind words of advice. I, too have been very frustrated with some of the answers... but I know it is in my best interest to practice discipline (you know, the "bite your tongue" expression?). Further, on several occasions, the answer that frustrates the most, sometimes ends up being the correct answer as I continue to evolve and develop my PHP skills. Peace. Quote Link to comment https://forums.phpfreaks.com/topic/312976-help-about-loop-in-php/#findComment-1587610 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.