473,626 Members | 3,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16195
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_reportin g/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.comman dline.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('CONSTAN T',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('CONSTAN T',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****@mlynarc zyk-webdesign.dewro te:
Rik Wasmus schrieb:
>define('CONSTA NT',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****@mlynar czyk-webdesign.dewro te:
>>Rik Wasmus schrieb:

define('CONS TANT',fopen('te st.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.da t', 'w');
define('myfile' , $f);
fwrite(myfile, "Line written to file\n");
fclose($f);
$f = fopen('test2.da t', '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.da t', '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.da t', '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
3567
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { $this->fileNameArray = array(); $this->isRemoved = 0; }
0
4657
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { // CONSTRUCTOR $this->fileNameArray = array(); $this->isRemoved = 0; }
11
17057
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 Category</a> either by calling a javascript or doing it inline as above. Thanks !!
1
1809
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 these solutions: a. Save from C++ program a temp file and load from PHP script b. PHP script stdin passed from my C++ program (I dont know how) c. Link with php5ts.dll (or .so) and pass the data (is it possible?) 2. When PHP script finished, I...
17
7909
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" requires user input to run; this causes the TCL script calling the PHP script to hose up and die. Is there a way I can do this so that the TCL script can call the PHP
17
2639
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 this script called) on Windows (I'm on Win XP Pro)? WScript/CScript (when programming in VBScript) allow this, for example. Failing that, is there any way to conclusively determine whether the script that is running was invoked from php.exe or...
27
5119
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 contents ]; close $fileID
5
6995
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 is gonna be exec. I cant seem to understand how can i pass variables from the PHP script to the Perl script and vice versa using exec? Is there a better way to go about it? Rahul
6
2434
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 array. How do I get this array to another PHP shell script? Can I use pipe? /etc/getAllPathsForDomain 'thesecondroad.org' | /etc/ findFilesUpdatedInLast24Hours Is the syntax basically right? Is this how I should build it?
0
8268
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
8202
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
8707
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
8641
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...
1
8366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5575
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();...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
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.