PHP output streams

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

PHP output streams

Post by quocbao »

Hi , i have some small questions

What different between using echo and php output stream ?
What different between php://stdout and php://output
Why use php stream and which method will be better for output ?

Code: Select all

<?

$f = fopen("php://output" , "w");

fwrite($f,"Hello world");

fclose($f);



?>

Code: Select all

<?

echo "Hello world";

?>
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Let's see using fopen adds alot of extra unneeded overhead, slower and just the worst way ever to send output to a browser.

It's like driving 5 miles out of your way to get from point A to point B when you could have diven 100 feet in a straight line to get from point A to point B.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP output streams

Post by Christopher »

quocbao wrote:What different between using echo and php output stream ?
Nothing as they have the same result.
quocbao wrote:What different between php://stdout and php://output
See http://us3.php.net/manual/en/wrappers.php.php or more general http://us3.php.net/manual/en/wrappers.php
quocbao wrote:Why use php stream and which method will be better for output ?
If you are outputting to the HTTP Response (or stdout in command line) then echo() is the simplest way to output text. Read/writing to files, sockets, etc. is when you need to use other functions.

There also is a standard Streams library http://www.php.net/manual/en/ref.stream.php
(#10850)
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post by quocbao »

php://stdin, php://stdout and php://stderr allow access to the corresponding input or output stream of the PHP process.
php://output allows you to write to the output buffer mechanism in the same way as print() and echo().
That's clear :)

Thanks :roll: !
Post Reply