The concept behind this is to use the built-in OS standard input stream protocol of PHP and feed it through fopen. Then get the value using fgets (getting a line from the current file pointer) which is typically the latest input received within the PHP runtime. The application will pause / stall because it's expecting an input with the currently open input stream protocol which makes it similar to the prompt function or language construct by earlier programming languages.
Here's how to do it.
<?php
$fh = fopen("php://stdin", "r");
echo "Enter Input:";
$input = fgets($fh);
//do something about $input
Or, to save yourself from opening the stdin stream, you can use the already open one built-in to PHP stored in STDIN, you can use the following snippet
<?php
echo "Enter Input:";
$input = fgets(STDIN);
//do something about $input
?>
This is yet another proof of PHP being able to do things that commonly being thought-of that it is something it can't do.
No comments:
Post a Comment