473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Very weird for echo/print and fgets ...

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
3 2965
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

"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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Schott | last post by:
Hi .. this is for sure the 1k question on fopen but maybe can someone tell me this weird staff if i'm writing: $H = fopen($value,"r"); while(!feof($H)) { $string = fgets($H,1024); .... .......
3
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed...
6
by: Brian | last post by:
Here's the example: You have a world writable file called lastquote.cfg on your server with a one or two digit number in it. When you run the script below, it reads the value, increments the...
13
by: Tony | last post by:
I'm just wondering why it seems most people use "echo" instead of "print". I tend to use "print", probably because I started programming in BASIC back in 78, so it's just familiar. Echo can take...
1
by: Jeff Silverman | last post by:
I have a PHP program that almost works. I'm running it from the command line and simulating a form using a GET method. That part is working, but I get spurious records with all of the fields...
5
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)"...
7
by: dtschoepe | last post by:
Hi, I am working on a project for school and I am trying to get my head around something weird. I have a function setup which is used to get an input from stdin and a piece of memory is created...
0
by: hadge | last post by:
Ok so i have a PHP/SQL menu, and a PHP/SQL script that pulls posts from the database. When the user posts they choose which page they want to post to ie index. Well i can get the page to display...
0
by: milan.topalov | last post by:
I'm having some strange problems using fopen() to fetch website content. It seems I can't open URL shorter then certain lenght. I can open this:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.