Skip to content

Instantly share code, notes, and snippets.

@me-shaon
Created September 11, 2023 17:06
Show Gist options
  • Save me-shaon/10b9164badc98e013cb1f750f065e29a to your computer and use it in GitHub Desktop.
Save me-shaon/10b9164badc98e013cb1f750f065e29a to your computer and use it in GitHub Desktop.
This is a simple CLI app to guess a number, written as a demo for my students
#! /usr/bin/env php
<?php
// Guessing game CLI app
$options = getopt('h::', ["min::", "max::"]);
if (isset($options['h'])) {
printf("This is a guessing game application");
} else {
$min = (int) ($options['min'] ?? 1);
$max = (int) ($options['max'] ?? 100);
$number = rand($min, $max);
while(true) {
$user_input = (int) readline("Enter a number: ");
if ($user_input > $number) {
printf("Try a lower number.\n");
} else if ($user_input < $number) {
printf("Try a bigger number.\n");
} else {
printf("Congrats! You guessed it right!");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment