Tools, FAQ, Tutorials:
Reading Data from Keyboard in PHP
How To Read Data from Keyboard (Standard Input) in PHP?
✍: FYIcenter.com
If you want to read data from the standard input, usually the keyboard, you can use the fopen("php://stdin") function. It creates a special file handle linking to the standard input, and returns the file handle. Once the standard input is opened to a file handle, you can use fgets() to read one line at a time from the standard input like a regular file. Remember fgets() also includes "\n" at the end of the returning string. Here is a PHP script example on how to read from standard input:
<?php
$stdin = fopen("php://stdin", "r");
print("What's your name?\n");
$name = fgets($stdin);
print("Hello $name!\n");
fclose($stdin);
?>
This script will print:
What's your name? Leo Hello Leo !
"!" is showing on the next line, because $name includes "\n" returned by fgets(). You can use rtrim() to remove "\n".
If you are using your script in a Web page, there is no standard input.
If you don't want to open the standard input as a file handle yourself, you can use the constant STDIN predefined by PHP as the file handle for standard input.
⇒ Opening Standard Output as a File Handle in PHP
⇐ Writing an Array to a File without File Handle in PHP
2024-01-16, ≈16🔥, 5💬
Popular Posts:
How To Get the Minimum or Maximum Value of an Array in PHP? If you want to get the minimum or maximu...
What is EPUB 2.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 2.0 Metadata "dc:publisher" ...
How to add request URL Template Parameters to my Azure API operation 2017 version to make it more us...
How to install "The Windows SDK version 8.1"? I need to build my Visual Studio C++ applications. If ...
How to view API details on the Publisher Dashboard of an Azure API Management Service? You can follo...