settingsAccountsettings
Menusettings

Q: Numbers from N to 1 in PHP

+1 vote

You are given a number num. Write a PHP script that loops through all of the numbers from num to 1 and prints them. The input comes as a parameter named num. The parameter num will hold a positive integer.

Examples:

Parameter name

Input

Output

num

5

5

4

3

2

1

 

Input

Output

2

2

1

 

asked in PHP category by user golearnweb

1 Answer

+1 vote
 
Best answer

Here is the answer:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<form>
    N: <input type="text" name="num"/>
    <input type="submit"/>
</form>
<?php
if (isset($_GET['num'])) {
    $num = intval($_GET['num']);

    for ($i = $num; $i >= 1; $i--) {
        echo $i . " ";
    }
}
?>
</body>
</html>

 

answered by user richard8502
selected by user golearnweb
...