How to print colors on ODD numbers?


#1

Hello, I have created a code that writes numbers from 20-49 on a line, I need help to write the odd numbers in red.

<?php


for ($i=20; $i<=49; $i++){      
    echo "$i<br>";  
} 

?>


#2

Hello realbaggyfan,

I have solved your query regarding odd numbers in color format. Please use below mentioned code for your requirement.

<html>
<body>
<?php
for($num=20;$num<=49;$num++)
if($num%2!=0)
echo “<p><font color=red font face=‘arial’ size=‘2pt’>$num</font></p>”;
else
echo “<p><font color=black font face=‘arial’ size=‘2pt’>$num</font></p>”;
?>

</body>
</html>

Kindly give your suggestions regarding this.


#3

Ahhh, I see! Thank you so much for taking your time helping me. Appreciate it big time!


#4

So, your solution is to use outdated html with attributes that don’t exist? And to duplicate it?

$class = $num % 2 == 0 ? 'even' : 'odd';
echo "<p class='{$class}'>{$i}</p>";

#5

My kindly suggestion is to stop using deprecated/obsolete code from the 90’s. We use CSS now as @astonecipher’s example shows.


#6

Do you even need PHP and classes? surely some of the nth-child css selectors have to be evergreen by now.