PHP CLI: Ask the User a Yes/No Question and Return Their Response


PHP Code
 function Ask_YN($Prompt = '', $Default = null) {
		/*
		Purpose
			Ask the user a question that will result in a "Y" or "N" answer and return the boolean result.
		Parameters
			Prompt (optional string) = The prompt to display to the user.
				If Default is true, the string " [Y/n] " will automatically be appended to this string.
				If Default is false, the string " [y/N] " will automatically be appended to this string.
				Default: "" (zero-length string)
			Default (optional string) = The value to return if the user presses enter without pressing a key.
				Default if null: false
				Default: null
		Return (boolean)
			Function returns true if the user presses "Y" or "y".
			Function returns false if the user presses "N" or "n".
			Function returns the value of Default if the user presses Enter.
		Version History
			1.0 2012.04.03  Tested by Russ Tanner on PHP 5.3.10.
		*/

		// Default parameters
		if (is_null($Default)) $Default = false;
		
		// Append Y/n prompt to Prompt.
		if (strlen($Prompt) > 0) $Prompt .= ' ';				// If Prompt has characters, append a spacer for [Y/n].
		$Prompt .= ($Default ? '[Y/n] ' : '[y/N] ');
		
		// Main Loop: Loop until a valid response is provided.
		while (true) {
		
			// Get user input.
			print $Prompt;
			$in = chop(fgets(STDIN));  
			
			// The user entered ENTER. Return the default value and exit now.
			if ($in == '') return $Default;
			// The user entered "Y" or "y". Return true and exit now.
			if ($in == 'Y' || $in == 'y') return true;
			// The user entered "N" or "n". Return false and exit now.
			if ($in == 'N' || $in == 'n') return false;
			
		}

	} 


The Captian
Today they call you "crazy". Tomorrow they call you "ahead of your time."
Global Skywatch Learn about Chemtrails - You're breathing them now!
OnlyTheBestHerbs.com World-class supplements
Mercury Talk Why you are sick.
OneUp Domains Domains, Hosting, Email
1-800-358-4278 (U.S. & Canada)