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

Very weird for echo/print and fgets ...

P: n/a
Hi all,

Please take a look at following very simple script;

#!/usr/bin/php -q
<?
echo "Hello, world!\n";
if (!$tty) {
$tty = fopen('php://stdin', 'r');
}
print "Hello: ";
$ch = fgets($tty, 1024);
print "$ch\n";
?>

I tested it on many servers. Here is how it running (you can
figure out yourself): Firstly it displays "Hello, world!", then
displays "Hello: " at second line and waits for user input;
You input a "yes" and it displays the "yes"; then end. So, on
the screen you will see:

Hello, world!
Hello: yes
yes

However, on one of the servers, it doesn't run like above,
but in a reversed way: Firstly, it displays nothing but waits
for your input; you input a "yes", then it displays the "yes",
then "Hello, world!", then "Hello: yes". So, on the screen it
looks like this:

yes
Hello, world!
Hello: yes

Weird? To me IT IS! I really don't understand why all the
echos/prints did not function until the script got data from
tty, even they are before fgets..

I found this issue during an unsuccessfull pear installation.
The pear installer stopped and waited for my input without
any prompt - there is prompt message but not displayed!

Can anyone let me know how this can happen? Thanks
in advance!

Alex

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Alex Shi wrote:
#!/usr/bin/php -q
<?
echo "Hello, world!\n";
if (!$tty) {
$tty = fopen('php://stdin', 'r');
}
print "Hello: ";
$ch = fgets($tty, 1024);
print "$ch\n";
?> <snip> However, on one of the servers, it doesn't run like above,
but in a reversed way: Firstly, it displays nothing but waits
for your input; you input a "yes", then it displays the "yes",
then "Hello, world!", then "Hello: yes". So, on the screen it
looks like this:

yes
Hello, world!
Hello: yes

Weird? To me IT IS! I really don't understand why all the
echos/prints did not function until the script got data from
tty, even they are before fgets..


This would make sense if there is buffering enabled in php.ini. Compare
buffering settings between the servers that work as you expect and the
server that doesn't. Then try turning off buffering and try again.
Jul 17 '05 #2

P: n/a

"Alex Shi" <ch****@stonix.com> wrote in message
news:of*****************@nntp-post.primus.ca...
Hi all,

Please take a look at following very simple script;

#!/usr/bin/php -q
<?
echo "Hello, world!\n";
if (!$tty) {
$tty = fopen('php://stdin', 'r');
}
print "Hello: ";
$ch = fgets($tty, 1024);
print "$ch\n";
?>

I tested it on many servers. Here is how it running (you can
figure out yourself): Firstly it displays "Hello, world!", then
displays "Hello: " at second line and waits for user input;
You input a "yes" and it displays the "yes"; then end. So, on
the screen you will see:

Hello, world!
Hello: yes
yes

However, on one of the servers, it doesn't run like above,
but in a reversed way: Firstly, it displays nothing but waits
for your input; you input a "yes", then it displays the "yes",
then "Hello, world!", then "Hello: yes". So, on the screen it
looks like this:

yes
Hello, world!
Hello: yes

Weird? To me IT IS! I really don't understand why all the
echos/prints did not function until the script got data from
tty, even they are before fgets..

I found this issue during an unsuccessfull pear installation.
The pear installer stopped and waited for my input without
any prompt - there is prompt message but not displayed!

Can anyone let me know how this can happen? Thanks
in advance!

Alex


Hmmm, stdout and stdin are supposed to be linked, so that stdout is
automatically flushed when stdin is read from. Obviously it isn't working on
this particular server, or it's a bug in PHP. Try flushing the output buffer
manually with flush() before calling fgets().
Jul 17 '05 #3

P: n/a
> Alex Shi wrote:
#!/usr/bin/php -q
<?
echo "Hello, world!\n";
if (!$tty) {
$tty = fopen('php://stdin', 'r');
}
print "Hello: ";
$ch = fgets($tty, 1024);
print "$ch\n";
?>

<snip>
However, on one of the servers, it doesn't run like above,
but in a reversed way: Firstly, it displays nothing but waits
for your input; you input a "yes", then it displays the "yes",
then "Hello, world!", then "Hello: yes". So, on the screen it
looks like this:

yes
Hello, world!
Hello: yes

Weird? To me IT IS! I really don't understand why all the
echos/prints did not function until the script got data from
tty, even they are before fgets..


This would make sense if there is buffering enabled in php.ini. Compare
buffering settings between the servers that work as you expect and the
server that doesn't. Then try turning off buffering and try again.


Your are right! I disabled output_buffering then it works the
correct way :)

Alex

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.