From: Marten Lehmann on
Hello,

I found different code examples like this, which use the file handle
STDERR just like this:

<?php
fwrite(STDERR, "hello\n");
?>

Also, the PHP documentation of input/output streams
(http://php.net/manual/de/wrappers.php.php) says:

"It is recommended that you simply use the constants STDIN, STDOUT and
STDERR instead of manually opening streams using these wrappers."

I don't want to use the "php://stderr" wrapper, because this just
creates a duplicate of the original STDERR handler and if I'm closing
"php://stderr", the original STDERR still would exist.

When I'm using this code, I only get:

<b>Notice</b>: Use of undefined constant STDERR - assumed 'STDERR' in
<b>/test.php</b> on line <b>4</b><br />
<br />
<b>Warning</b>: fwrite(): supplied argument is not a valid stream
resource in <b>/test.php</b> on line <b>4</b><br />

How can I access the original STDERR handle? The constant should be
there, but does not exist.

regards
Marten
From: "Jan G.B." on
2010/3/23 Marten Lehmann <lehmann(a)cnm.de>

> Hello,
>
> I found different code examples like this, which use the file handle STDERR
> just like this:
>
> <?php
> fwrite(STDERR, "hello\n");
> ?>
>
> Also, the PHP documentation of input/output streams (
> http://php.net/manual/de/wrappers.php.php) says:
>
> "It is recommended that you simply use the constants STDIN, STDOUT and
> STDERR instead of manually opening streams using these wrappers."
>
> I don't want to use the "php://stderr" wrapper, because this just creates a
> duplicate of the original STDERR handler and if I'm closing "php://stderr",
> the original STDERR still would exist.
>
> When I'm using this code, I only get:
>
> <b>Notice</b>: Use of undefined constant STDERR - assumed 'STDERR' in
> <b>/test.php</b> on line <b>4</b><br />
> <br />
> <b>Warning</b>: fwrite(): supplied argument is not a valid stream resource
> in <b>/test.php</b> on line <b>4</b><br />
>
> How can I access the original STDERR handle? The constant should be there,
> but does not exist.
>
> regards
> Marten
>
>
Hi,

I can reproduce it with some differences. Check this out:

$ php --version
PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli) (built: Jan 6 2010
22:56:44)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

$ php 1>/dev/null # note that the PHP error messages are
displayed on STDOUT - I don't show them with "1>/dev/null"
<?
$x = fopen('php://stderr', 'r'); fwrite($x, "test\n"); fclose($x);
$x = fopen(STDERR, 'r'); fwrite($x, "test2\n"); fclose($x); ?>
------------
<?
fwrite(STDERR, "test3\n");
fwrite ('php://stderr', "test4");
?>
-----------

Result:

test


--------

So my assumption is, that the quoted recommendation is outdated / wrong.

Regards


PS: PHP reports this when giving the constant STDERR:
Warning: fopen(STDERR): failed to open stream: No such file or directory
Warning: fwrite(): supplied argument is not a valid stream resource
From: Daniel Egeberg on
On Tue, Mar 23, 2010 at 11:47, Marten Lehmann <lehmann(a)cnm.de> wrote:
> Hello,
>
> I found different code examples like this, which use the file handle STDERR
> just like this:
>
> <?php
> fwrite(STDERR, "hello\n");
> ?>
>
> Also, the PHP documentation of input/output streams
> (http://php.net/manual/de/wrappers.php.php) says:
>
> "It is recommended that you simply use the constants STDIN, STDOUT  and
> STDERR instead of manually opening streams using these wrappers."
>
> I don't want to use the "php://stderr" wrapper, because this just creates a
> duplicate of the original STDERR handler and if I'm closing "php://stderr",
> the original STDERR still would exist.
>
> When I'm using this code, I only get:
>
> <b>Notice</b>:  Use of undefined constant STDERR - assumed 'STDERR' in
> <b>/test.php</b> on line <b>4</b><br />
> <br />
> <b>Warning</b>:  fwrite(): supplied argument is not a valid stream resource
> in <b>/test.php</b> on line <b>4</b><br />
>
> How can I access the original STDERR handle? The constant should be there,
> but does not exist.
>
> regards
> Marten

These I/O streams are only present in the CLI SAPI.

--
Daniel Egeberg
From: "Jan G.B." on
2010/3/23 Daniel Egeberg <degeberg(a)php.net>

> On Tue, Mar 23, 2010 at 11:47, Marten Lehmann <lehmann(a)cnm.de> wrote:
> > Hello,
> >
> > I found different code examples like this, which use the file handle
> STDERR
> > just like this:
> >
> > <?php
> > fwrite(STDERR, "hello\n");
> > ?>
> >
> > Also, the PHP documentation of input/output streams
> > (http://php.net/manual/de/wrappers.php.php) says:
> >
> > "It is recommended that you simply use the constants STDIN, STDOUT and
> > STDERR instead of manually opening streams using these wrappers."
> >
> > I don't want to use the "php://stderr" wrapper, because this just creates
> a
> > duplicate of the original STDERR handler and if I'm closing
> "php://stderr",
> > the original STDERR still would exist.
> >
> > When I'm using this code, I only get:
> >
> > <b>Notice</b>: Use of undefined constant STDERR - assumed 'STDERR' in
> > <b>/test.php</b> on line <b>4</b><br />
> > <br />
> > <b>Warning</b>: fwrite(): supplied argument is not a valid stream
> resource
> > in <b>/test.php</b> on line <b>4</b><br />
> >
> > How can I access the original STDERR handle? The constant should be
> there,
> > but does not exist.
> >
> > regards
> > Marten
>
> These I/O streams are only present in the CLI SAPI.
>
> --
> Daniel Egeberg
>
> Please confirm that the code of my previous replay on this topic (executed
via php-cli) does not print out "test2" or "test3". The constant is present
but doesn't work as supposed.

Regards
From: Daniel Egeberg on
On Tue, Mar 23, 2010 at 15:50, Jan G.B. <ro0ot.w00t(a)googlemail.com> wrote:
>
>
> 2010/3/23 Daniel Egeberg <degeberg(a)php.net>
>>
>> On Tue, Mar 23, 2010 at 11:47, Marten Lehmann <lehmann(a)cnm.de> wrote:
>> > Hello,
>> >
>> > I found different code examples like this, which use the file handle
>> > STDERR
>> > just like this:
>> >
>> > <?php
>> > fwrite(STDERR, "hello\n");
>> > ?>
>> >
>> > Also, the PHP documentation of input/output streams
>> > (http://php.net/manual/de/wrappers.php.php) says:
>> >
>> > "It is recommended that you simply use the constants STDIN, STDOUT  and
>> > STDERR instead of manually opening streams using these wrappers."
>> >
>> > I don't want to use the "php://stderr" wrapper, because this just
>> > creates a
>> > duplicate of the original STDERR handler and if I'm closing
>> > "php://stderr",
>> > the original STDERR still would exist.
>> >
>> > When I'm using this code, I only get:
>> >
>> > <b>Notice</b>:  Use of undefined constant STDERR - assumed 'STDERR' in
>> > <b>/test.php</b> on line <b>4</b><br />
>> > <br />
>> > <b>Warning</b>:  fwrite(): supplied argument is not a valid stream
>> > resource
>> > in <b>/test.php</b> on line <b>4</b><br />
>> >
>> > How can I access the original STDERR handle? The constant should be
>> > there,
>> > but does not exist.
>> >
>> > regards
>> > Marten
>>
>> These I/O streams are only present in the CLI SAPI.
>>
>> --
>> Daniel Egeberg
>>
> Please confirm that the code of my previous replay on this topic (executed
> via php-cli) does not print out "test2" or "test3". The constant is present
> but doesn't work as supposed.
> Regards
>
>

You are making a number of errors there:
1) You are trying to open php://stderr in read mode, but that stream
is write only (I'm not sure why it outputs regardless).
2) You are trying to open STDERR, but that constant holds a stream
resource while fopen() expects a string.
3) You are trying to write to 'php://stderr'. That's not possible. You
cannot pass a string as stream.

The following should work:

daniel(a)daniel-laptop:~$ cat test.php
<?php
$stderr = fopen('php://stderr', 'w');
fwrite($stderr, 'Error 1' . PHP_EOL);
fwrite(STDERR, 'Error 2' . PHP_EOL);
echo 'Normal echo';
daniel(a)daniel-laptop:~$ php test.php > /dev/null
Error 1
Error 2
daniel(a)daniel-laptop:~$ ./src/php-5.2.12/sapi/cli/php test.php > /dev/null
Error 1
Error 2

--
Daniel Egeberg