-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor.php
More file actions
40 lines (33 loc) · 1.29 KB
/
for.php
File metadata and controls
40 lines (33 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
// Create a file named for.php in your exercises repo. Commit and push all changes after each step.
// Prompt user for a starting number and ending number, then display all the numbers from the starting to ending using a for loop.
// Refactor to allow user to choose increment. (count by 1, 2, 10, ...)
fwrite(STDOUT, 'What\'s the starting number? ');
$startingNumber = trim(fgets(STDIN));
if (! is_numeric($startingNumber)) {
echo 'You must enter a number!' . PHP_EOL;
exit;
}
fwrite(STDOUT, 'What\'s the ending number? ');
$endingNumber = trim(fgets(STDIN));
if (! is_numeric($endingNumber)) {
echo 'You must enter a number!' . PHP_EOL;
exit;
}
fwrite(STDOUT, 'What\'s the increment? ');
$increment = trim(fgets(STDIN));
if (! is_numeric($increment)) {
echo 'You must enter a number!' . PHP_EOL;
exit;
}
// Default increment to 1 if no input.
if ($increment == "") {
$increment = 1;
}
// then display all the numbers from the starting to ending using a for loop.
// Refactor to allow user to choose increment. (count by 1, 2, 10, ...)
for($i = $startingNumber; $i <= $endingNumber; $i += $increment) {
echo $i . PHP_EOL;
}
// Make sure you are only allowing users to pass in numbers. Give an error
// message is both passed arguments are not numeric. See php.net/is_numeric.