How To Read Data from Keyborad (Standard Input)
How To Read Data from Keyborad (Standard Input)? - PHP Script Tips - Reading and Writing Files
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.
2007-04-23, 4548👍, 0💬
Describe in detail Basic of SAO architecture of Remoting? For these types of questions interviewer e...
Can you explain different software development life cycles -part II? Water Fall Model This is the ol...
How To Control Vertical Alignment? - XHTML 1.0 Tutorials - Understanding Tables and Table Cells By d...
How To Enter Microseconds in SQL Statements? - MySQL FAQs - Introduction to SQL Date and Time Handli...
How To Export Data to a CSV File? - Oracle DBA FAQ - Introduction to Oracle SQL Developer If you wan...