By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

How to read from stdin using PHP

P: n/a
In TCL it would be written this way:

puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar
I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}
I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
In TCL it would be written this way:

> puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

> BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil


A cursory search found this in the fopen() help:

// read_data()
// gets a line of data from STDIN and returns it
function read_data() {

$in = fopen("php://stdin", "r");
set_timeout();
$in_string = fgets($in, 255);
clear_timeout();
fclose($in);
return $in_string;
}

Enjoy.

Garp
Jul 17 '05 #2

P: n/a
Phil Powell wrote:
In TCL it would be written this way:

> puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

> BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil

First, is this an interactive-type system where you are using PHP in
place of VB or sh or an executable? There is a lot missing from this
dialogue. PHP is not a Visual-anything application, it was designed as
an HTML scripting language to create/display dynamic content and present
it in a browser but some are starting to use it as a cli scripting language.

If this is a web-based PHP app, you do not have the same concepts as an
interactive session. What you can do to mimick this behavior (ie "Do
you really want to..") is to display the results of the fields entered
on a "verification" page and continue if everything looks correct -
executing the form.

Web-based (where your browser is your viewer) pages are static - in that
they stand alone and do not have interaction with the system unless
you submit a form to that system using either POST or GET - in which
case, you get another page with additional forms etc...

now to answer your question.

see: << http://www.zend.com/zend/spotlight/shellscriptingp1.php

Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>
Michael Austin.
Jul 17 '05 #3

P: n/a
Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>
Michael Austin.


Um.. with respect to that shebang statement. Is that necessary? I've been
writing all of my files in PHP without it and while it works, I'd like my
code to be standardized. Is that really supposed to be there? I know it's a
perl thing -- didn't realize you use it in PHP too...

-- Matt
Jul 17 '05 #4

P: n/a
On Thu, 1 Jul 2004 16:29:23 -0400, "Matthew Del Buono" <Ma******@nospam.com>
wrote:
#!/usr/local/bin/php -q


Um.. with respect to that shebang statement. Is that necessary? I've been
writing all of my files in PHP without it and while it works, I'd like my
code to be standardized. Is that really supposed to be there? I know it's a
perl thing -- didn't realize you use it in PHP too...


It's not a Perl thing, it's a Unix thing, and is required if you want to be
able to run as "./script.php" rather than "php ./script.php".

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

P: n/a
Michael Austin <ma*****@firstdbasource.com> wrote in message news:<eM*****************@newssvr23.news.prodigy.c om>...
Phil Powell wrote:
In TCL it would be written this way:

> > puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

> > BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil

First, is this an interactive-type system where you are using PHP in
place of VB or sh or an executable? There is a lot missing from this
dialogue. PHP is not a Visual-anything application, it was designed as
an HTML scripting language to create/display dynamic content and present
it in a browser but some are starting to use it as a cli scripting language.

If this is a web-based PHP app, you do not have the same concepts as an
interactive session.


[snip]

NO this is never going to be web-based; this is pure CLI scripting
version of PHP, thus, I am having to use stdin as if I were coding in
TCL, Java or even Shell Script. I wound up figuring it out though
using a variation of your example to do so in PHP.

Thanx
Phil
now to answer your question.

see: << http://www.zend.com/zend/spotlight/shellscriptingp1.php

Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>
Michael Austin.

Jul 17 '05 #6

P: n/a
Phil Powell wrote:
Michael Austin <ma*****@firstdbasource.com> wrote in message news:<eM*****************@newssvr23.news.prodigy.c om>...
Phil Powell wrote:

In TCL it would be written this way:

>>>puts {Do you want to do this (Y/N Enter=N)?}
gets stdin deleteChar

I guess what I need is the PHP equivalent of TCL's gets command (see
http://www.astro.princeton.edu/~rhl/...a1/gets.n.html
for more information)

or perhaps the PHP equivalent of how a Java app would read from
standard in:

>>>BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((in.readLine() == null)) {}

I checked out the PHP streaming capabilities and I have no clue how to
use them and the manual makes no clear mention of how they can be used
to retrieve from stdin:

http://www.php.net/wrappers.php

Help appreciated, this is driving me nuts!

Thanx
Phil

First, is this an interactive-type system where you are using PHP in
place of VB or sh or an executable? There is a lot missing from this
dialogue. PHP is not a Visual-anything application, it was designed as
an HTML scripting language to create/display dynamic content and present
it in a browser but some are starting to use it as a cli scripting language.

If this is a web-based PHP app, you do not have the same concepts as an
interactive session.

[snip]

NO this is never going to be web-based; this is pure CLI scripting
version of PHP, thus, I am having to use stdin as if I were coding in
TCL, Java or even Shell Script. I wound up figuring it out though
using a variation of your example to do so in PHP.

Thanx
Phil


Glad I, ummm, err... Google could be of assistance :)...

now to answer your question.

see: << http://www.zend.com/zend/spotlight/shellscriptingp1.php

Example:

#!/usr/local/bin/php -q
<?php
function getInput($length=255)
{
$fr=fopen("php://stdin","r");
$input = fgets($fr,$length);
$input = rtrim($input);
fclose ($fr);
return $input;
}

echo "Enter some text (10 char max);
$text = getInput(10);
echo "you entered \n $text\n";
?>


Michael Austin.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.