php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #21428 additional example for CLI SAPI page
Submitted: 2003-01-05 07:08 UTC Modified: 2004-07-27 15:40 UTC
From: andrey@php.net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.0 OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: andrey@php.net
New email:
PHP Version: OS:

 

 [2003-01-05 07:08 UTC] andrey@php.net
Hi,
few minutes ago I was asked by a friend :
"How can I read the user input in CLI?"
He proposed this :

<?php
print "enter:\n";
$r = fread(STDIN, 100);
print $r;
?>

but....the user has to enter EOF (Ctrl-Z on Windows, Ctrl-D on *nix). This
is not intuitive.
So after some thinking I created this :

<?php
print  "enter:\n";
fscanf($STDIN, "%d\n",$r); //reading a number
print  $r;
?>

I haven't found an example about that issue on the CLI SAPI page.

Thanks,
Andrey

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-05 07:11 UTC] andrey@php.net
sorry
STDIN instead of $STDIN
 [2003-01-05 07:21 UTC] nicos@php.net
Note: there should be an another one with fgets() IMHO.
 [2003-01-05 07:26 UTC] andrey@php.net
I completely agree.
fscanf() is for advanced users but one example with fgets() and fscanf() will be better IMHO.
 [2003-01-05 11:44 UTC] philip@php.net
If \r\n was used in the example, would this CLI example work fine on all OS's including macs? (which uses \r)
 [2003-01-05 11:48 UTC] tal@php.net
Ilia fixed the \r issues in the streams layer itself a while ago, so this should work on mac.
 [2004-07-27 15:40 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"If you want to read single line from stdin, you can use
<?php
$line = trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN
?>
"
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Nov 19 11:00:01 2025 UTC