Recipe 23.14. Writing to Standard Output

23.14.1. Problem

You want to write to standard output.

23.14.2. Solution

Use echo or print( ) print, as in Example 23-36.

Writing to standard output

<?php print "Where did my pastrami sandwich go?"; echo  "It went into my stomach."; ?>

23.14.3. Discussion

While print( ) is a function, echo is a language construct. This means that print( ) returns a value, while echo doesn't. You can include print( ) but not echo in larger expressions, as shown in Example 23-37.

echo versus print

<?php // this is OK (12 == $status) ? print 'Status is good' : error_log('Problem with status!'); // this gives a parse error (12 == $status) ? echo 'Status is good' : error_log('Problem with status!'); ?>

Use php://stdout as the filename if you're using the file functions $fh = fopen('php://stdout','w') or die($php_errormsg);.

Writing to standard output via a filehandle instead of simply with print( ) or echo is useful if you need to abstract where your output goes, or if you need to print to standard output at the same time as writing to a file. See Recipe 23.15 for details.

You can also write to standard error by opening php://stderr: $fh = fopen('php://stderr','w');.

23.14.4. See Also

Recipe 23.15 for writing to many filehandles simultaneously; documentation on echo at and on print( ) at

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: