Jump to content

Help about Loop in PHP


Lumana
 Share

Recommended Posts

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   

Link to comment
Share on other sites

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>

screenshot-localhost-2021.06.26-11_29_07.png

Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

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>';
  }
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

12 hours ago, Barand said:

The modulo operator was invented for situations like this


$mytext = "I &#10084; 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.