Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Created November 6, 2012 23:31
Show Gist options
  • Save jtpaasch/4028465 to your computer and use it in GitHub Desktop.
Save jtpaasch/4028465 to your computer and use it in GitHub Desktop.
Simple PHP script that can be run from the command line. It illustrates input/output/error usage.
<?php
/**
* If you call this with "$ php standard-in-out-error.php",
* it will print output and error messages to the command line.
*
* To pipe just the error messages into their own file,
* use "$ php standard-in-out-error.php 2> error.log"
*/
// This prints 'Please enter your name' on the command line.
fwrite(STDOUT, "Please enter your name:\n");
// This captures what gets typed in and assigns it to $name.
// Note that STDIN includes the carriage return EOL character.
// Here I get rid of it with trim().
$name = trim(fgets(STDIN));
// Print to the command line again.
fwrite(STDOUT, "Hello $name \n");
// Write this to the standard error pipe.
fwrite(STDERR, "Uh oh! $name was here! \n");
// Exit with a status of 'OK'
exit(0);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment