Very weird for echo/print and fgets ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Shi

    #1

    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

  • Zurab Davitiani

    #2
    Re: Very weird for echo/print and fgets ...

    Alex Shi wrote:
    [color=blue]
    > #!/usr/bin/php -q
    > <?
    > echo "Hello, world!\n";
    > if (!$tty) {
    > $tty = fopen('php://stdin', 'r');
    > }
    > print "Hello: ";
    > $ch = fgets($tty, 1024);
    > print "$ch\n";
    > ?>[/color]
    <snip>[color=blue]
    > 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..[/color]

    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.

    Comment

    • Chung Leong

      #3
      Re: Very weird for echo/print and fgets ...


      "Alex Shi" <chpshi@stonix. com> wrote in message
      news:ofgLc.9665 $Rc4.2069@nntp-post.primus.ca. ..[color=blue]
      > 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[/color]

      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().


      Comment

      • Alex Shi

        #4
        Re: Very weird for echo/print and fgets ...

        > Alex Shi wrote:[color=blue]
        >[color=green]
        > > #!/usr/bin/php -q
        > > <?
        > > echo "Hello, world!\n";
        > > if (!$tty) {
        > > $tty = fopen('php://stdin', 'r');
        > > }
        > > print "Hello: ";
        > > $ch = fgets($tty, 1024);
        > > print "$ch\n";
        > > ?>[/color]
        > <snip>[color=green]
        > > 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..[/color]
        >
        > 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.[/color]

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

        Alex

        Comment

        Working...