473,414 Members | 1,590 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,414 software developers and data experts.

STDOUT vs. php://STDOUT

I'm trying to debug some code I haven't been in for years. It's in the 1and1
linux hosting environment.

Under
php -v
PHP 4.4.8 (cgi) (built: Mar 6 2008 18:09:06)

This code
<?php
fwrite(STDOUT, "trythis\r\n");
?>

executed thusly:
php -f trythis.php
does nothing.

Under
php5 -v
PHP 5.2.5 (cgi) (built: Apr 25 2008 14:13:32)

This code
<?php
fwrite(STDOUT, "trythis\r\n");
?>

executed thusly:
php5 -f trythis.php
Produces:
<br />
<b>Warning</b>: fwrite(): supplied argument is not a valid stream resource
in <b......./trythis.php</bon line <b>2</b><br />

Apparently I must now do something looking like this:

<?php
$STDOUT = fopen('php://stdout', 'w');
fwrite($STDOUT, "trythis\r\n");
?>

Did I miss something along the way??!? Is this because it's (cgi)? How do I
coerce it to run (cli)???

Thanks for any thoughts!
Jun 2 '08 #1
5 16182
On Sat, 17 May 2008 02:00:37 +0200, Dick Watson
<li**************@mind-enufalready-spring.comwrote:
This code
<?php
fwrite(STDOUT, "trythis\r\n");
?>
Under
PHP 4.4.8 (cgi) (built: Mar 6 2008 18:09:06)
does nothing.
Causes an error, which is not shown due to ini settings
(error_reporting/display_errors).
Under
PHP 5.2.5 (cgi) (built: Apr 25 2008 14:13:32)
Produces:
<br />
<b>Warning</b>: fwrite(): supplied argument is not a valid stream
resource
in <b......./trythis.php</bon line <b>2</b><br />
Apparently I must now do something looking like this:
<?php
$STDOUT = fopen('php://stdout', 'w');
php://output
Did I miss something along the way??!? Is this because it's (cgi)?
Yes.
How do I coerce it to run (cli)???
And the manual states
<http://www.php.net/manual/en/features.commandline.php>:
"CLI specific constants"

Allthough they lie (it works as a module), if you NEED it, get cli instead
of cgi.

BTW: a weird side effect I suspect are a result of these CLI constants,
which you shouldn't trust, is that you can define streams as resources and
make them global:
<?php
define('CONSTANT',fopen('test.txt','w');
function foo($string){
fwrite(CONSTANT,$string);
}
foo("this works in CLI & Modules");
?>

It's abusing a behaviour that's not supposed to be so (constants should
only hold scalars), and I certainly will not guarantee you this will work
n arbitrary versions of PHP, however, different versions I've worked with
had that 'feature'. If you need this feature to get some code running fast
(defining forementioned methoed and bulk replacing instanced of STDOUT
with $GLOBALS['STDOUT'] will work too), maybe it's worth to take a look if
CGI does too?
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #2
Rik Wasmus schrieb:
define('CONSTANT',fopen('test.txt','w');
It's abusing a behaviour that's not supposed to be so (constants should
only hold scalars), and I certainly will not guarantee you this will
work n arbitrary versions of PHP, however, different versions I've
worked with had that 'feature'.
You mean it *is* actually possible to define constants with non-scalar
values, contrary to what the manual says? Could they even be objects or
arrays? In which versions of PHP does this work?

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Jun 2 '08 #3
On Sat, 17 May 2008 14:56:23 +0200, Thomas Mlynarczyk
<th****@mlynarczyk-webdesign.dewrote:
Rik Wasmus schrieb:
>define('CONSTANT',fopen('test.txt','w');
It's abusing a behaviour that's not supposed to be so (constants should
only hold scalars), and I certainly will not guarantee you this will
work n arbitrary versions of PHP, however, different versions I've
worked with had that 'feature'.

You mean it *is* actually possible to define constants with non-scalar
values, contrary to what the manual says? Could they even be objects or
arrays?
Nope, AFAIK, this only works for stream resouces. You could define a
custom stream wrapper to objects or arrays, and check wether this works,
but it seems an awful lot of trouble for something that could be removed
from PHP at any time, and cheating scope is often a sign bad design.
In which versions of PHP does this work?
I haven't met a PHP4 / 5 version yet in which this didn't work. I did not
test all of them though.
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #4
On Sun, 18 May 2008 04:15:43 +0200, ljb <lj*****@pobox.comwrote:
lu************@hotmail.com wrote:
>On Sat, 17 May 2008 14:56:23 +0200, Thomas Mlynarczyk
<th****@mlynarczyk-webdesign.dewrote:
>>Rik Wasmus schrieb:

define('CONSTANT',fopen('test.txt','w');
It's abusing a behaviour that's not supposed to be so (constants
should
only hold scalars), and I certainly will not guarantee you this will
work n arbitrary versions of PHP, however, different versions I've
worked with had that 'feature'.

You mean it *is* actually possible to define constants with non-scalar
values, contrary to what the manual says? Could they even be objectsor
arrays?

Nope, AFAIK, this only works for stream resouces. You could define a
custom stream wrapper to objects or arrays, and check wether this works,
but it seems an awful lot of trouble for something that could be removed
from PHP at any time, and cheating scope is often a sign bad design.
>>In which versions of PHP does this work?

I haven't met a PHP4 / 5 version yet in which this didn't work. I did
not
test all of them though.

The manual says "Do not define resource constants", but the PHP define()
function is written to accept resources (of any type). And the PHP CLI
module defines constants such as STDOUT with resources as values.

I think the explanation is that while resource constants work, PHP is
unaware of the defined constant when it manages whatever the resource
represents. So if the resource is freed using a function or when its
variable goes out of scope, the constant is now indeterminate. It might
point to nothing, or it might point to another resource that re-used the
integer value. For example:
$f = fopen('test1.dat', 'w');
define('myfile', $f);
fwrite(myfile, "Line written to file\n");
fclose($f);
$f = fopen('test2.dat', 'w');
fwrite(myfile, "Where will this go?\n");
(Try it without the second fopen. Can you explain what happens? I can't.)
A quick check here reveals that allthough fclose($f) is called, the stream
is NOT closed, and you can still use both $f and myfile to write. The same
goes the other way around with fclose(myfile), and then trying to write
with $f & myfile both work. Both the $f AND myfile have to be closed
before the stream is definitly closed / fwrite generates a warning. So
your premise about it being closed by either is wrong. Observe the
difference in output/test1.dat:
<?php
$f = fopen('test1.dat', 'w');
define('myfile', $f);
fwrite(myfile, "Line written to file\n");
var_dump(myfile,$f,$f===myfile);
fclose(myfile);
var_dump(myfile,$f,$f===myfile);
fwrite(myfile, "This will also go to file\n");
fclose(myfile);
fwrite(myfile, "Issues a warning, invalid stream\n");
?>
And:
<?php
$f = fopen('test1.dat', 'w');
$g = $f;
fwrite($g, "Line written to file\n");
var_dump($g,$f,$f===$g);
fclose($g);
var_dump($g,$f,$f===$g);
fwrite($g, "This will fail\n");
fclose($g);
fwrite($g, "And this one of course too\n");
?>

And this is all nice & theoretical, never ever define something elsethena
constant as a scalar. Quick run-once-and-this-saves-time code, maybe, but
not ever in a system meant to be run after that day.
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #5
Rik Wasmus schrieb:

[defining constants with non-scalars other than resources?]
Nope, AFAIK, this only works for stream resouces. You could define a
custom stream wrapper to objects or arrays, and check wether this works,
but it seems an awful lot of trouble for something that could be removed
from PHP at any time, and cheating scope is often a sign bad design.
I agree. However, it would be useful to be able to define arrays as
constants. I suppose, resources only work because the variable holds
just a handle, which internally is an integer and thus perfectly valid
for a constant.

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Jun 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { $this->fileNameArray = array(); $this->isRemoved = 0; }
0
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { // CONSTRUCTOR $this->fileNameArray = array(); $this->isRemoved = 0; }
11
by: Angelos | last post by:
Does anyone knows how to execute a PHP function using javascript.. What I want is to do that in the line bellow: <a href="" target="_blank" onClick="<? add_product_category(); ?>" >add...
1
by: | last post by:
I want to run PHP scripts from my C++ program. I can run "php -n my_script.php" but I have 2 problems: 1. From my C++ program I want to pass to PHP script a data structure (a C++ map). I think...
17
by: comp.lang.tcl | last post by:
The TCL command I am using will do a command-line action on a PHP script: set cannotRunPHP I have to do it this way as both the TCL script and the PHP script run as CLI. However, "info.php"...
17
by: Csaba Gabor | last post by:
Is there a way to determine the path to the php executable (as opposed to the script. In other words, I am looking for the path to php.exe or php-win.exe) that is currently running (ie. how was...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
5
by: rahulthathoo | last post by:
Hi. I need to develop a web based application which uses php for the front end and statistical manipulations in perl - my question is - how do i call a perl script from php - i guess the answer...
6
by: lawrence k | last post by:
Suppose I have a PHP script that I call at the Linux command line (Ubuntu), and it gets all the paths to files and directories that exist inside of some directory. All this data gets stored in an...
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
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,...
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
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...

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.