473,771 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2983
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_bufferin g 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
2380
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); .... .... blabla
3
2080
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 to get a working system just about finished. I am building an interface for our customer service folks to use to manage registered customers and am seeing some weird behavior.
6
1703
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 value by one and then rewrites it into the file. <?php $fp = fopen("lastquote.cfg", "r"); $data = fread($fp, 10); fclose($fp);
13
23260
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 multiple expressions (as in "echo $string1,$string2,$string3"), which apparently is a bit faster than multiple concatenations. But for basic output, there doesn't seem to be a noticeable difference. The speed test at...
1
1663
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 concatenated into a single field. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=windows-1252" http-equiv="content-type">
5
1715
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)" % self return {"a": "b", "c": "d"} and then I have another module called SensorSingleton that emulates the
7
2013
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 on the heap using malloc. This input is then run through a regex and another test. The result is passed back as a char pointer to the program for further processing The first time I call this function, it performs normally. However,
0
982
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 all the posts of the relevant pages, but if i try to put the menu in at the top of the page it all goes horribly wrong :( here is my php ro pull the data <? $result=mysql_query("SELECT * FROM north WHERE page='$page'"); while...
0
1666
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: http://groups.google.co.uk/groups/dir?lnk=od&h=en&sel=&x=sssssxsssq But if I delete a character from the end, fopen() failes after 20 sec and returns strange error:
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.