-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh_low.php
More file actions
35 lines (28 loc) · 737 Bytes
/
high_low.php
File metadata and controls
35 lines (28 loc) · 737 Bytes
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
<?php
$min = 1;
$max = 100;
if ($argc >= 3){
if (is_numeric($argv[1]) && is_numeric($argv[2]) && $argv[1] < $argv[2]) {
$min = $argv[1];
$max = $argv[2];
fwrite(STDOUT, "Guess a number between {$argv[1]} and {$argv[2]} see if you guessed correctly\n");
} else {
fwrite(STDERR, "You need to pass 2 numeric values\n");
exit(1);
}
}
$randomNumber = mt_rand($min, $max);
$guessCount = 0;
do {
fwrite(STDOUT, 'Guess: ');
$guess = trim(fgets(STDIN));
if($guess < $randomNumber){
fwrite(STDOUT, "HIGHER\n");
} elseif ($guess > $randomNumber){
fwrite(STDOUT, "LOWER\n");
} else {
fwrite(STDOUT, "GOOD GUESS!!\n");
}
$guessCount++;
} while ($guess != $randomNumber);
fwrite(STDOUT, "You guessed $guessCount times.\n");