settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Numbers from 1 to 20 in PHP

+4 votes

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
asked in PHP category by user eiorgert

1 Answer

+1 vote
 
Best answer

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>

 

answered by user golearnweb
selected by user golearnweb
...