[Solved] Numbers from 1 to 20 in PHP

  

3
Topic starter

Write a PHP script to print the numbers from 1 to 20:

  • Print the numbers in a list <ul><li>…</li></ul>
  • Print odd lines in blue, even lines in red
1 Answer
2

Here's the answer:

<!DOCTYPE html>
<html>
<head>
    <title>Hello PHP</title>
</head>
<body>
<ul>
    <?php
    for ($i = 1; $i <= 20; $i++) {
        if ($i % 2 == 1) {
            echo "<li><span style='color: blue'>$i</span></li>";
        } else {
            echo "<li><span style='color: green'>$i</span></li>";
 
        }
    }
    ?>
</ul>
</body>
</html>
Share: