Skip to content
Advertisement

Make number that is divisible by 7 the color red

I need to see if the numbers 1 – 1000 are divisible and make them the color red. But i’ve got no idea how to.

here’s my code:

<?php 
 for ($i=1; $i<=1000; $i++) {
   echo " $i <br/>"; 
 }
 if ($i % 7 != 0) {
  $i += 7 - ($i % 7);

Advertisement

Answer

<?php 
    for ($i=1; $i<=1000; $i++) { // iterate through the numbers
        if ($i % 7 != 0) // not divisible by 7, just print the number
             echo $i." <br/>"; 
        else //divisible by 7, print the number inside a span in order to color the text red
             echo '<span style="color:red">'.$i.'</span></br>'
   }  
?>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement