Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How To Read Data from Keyborad (Standard Input)
How To Read Data from Keyborad (Standard Input)? - PHP Script Tips - Reading and Writing Files
✍: 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.
2007-04-23, 4809👍, 0💬
Popular Posts:
What Are Named Parameters? - Oracle DBA FAQ - Creating Your Own PL/SQL Procedures and Functions Name...
How To Empty Your Recycle Bin? - Oracle DBA FAQ - Managing Oracle Database Tables If your recycle bi...
Can static variables be declared in a header file? You can't declare a static variable without defin...
How To Get the Last ID Assigned by MySQL? - MySQL FAQs - Managing Tables and Running Queries with PH...
What does static variable mean? There are 3 main uses for static variables: If you declare within a ...