How to print a line to STDERR and STDOUT in PHP?

In PHP, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely?

And similarly, how to print the line to STDERR?

In PHP, you may print a line to STDOUT using echo by appending the PHP_EOL to the string:

echo $your_msg . PHP_EOL;

For example,

$ php -a
Interactive shell

php > echo "hello world!" . PHP_EOL;
hello world!
php > 

To print to STDERR, you may use fwrite like

fwrite(STDERR, "hello, world!" . PHP_EOL);

One example is as follows.

$ cat php-stderr.php 
<?php
fwrite(STDERR, "hello, world!" . PHP_EOL);

$ php php-stderr.php 2>/tmp/stderr
$ cat /tmp/stderr 
hello, world!

Similar Posts

  • Specifying –no-print-directory within the Makefile

    The –no-print-directory option of make tells make not to print the message about entering and leaving the working directory. However, how to specify the –no-print-directory inside the Makefile itself? Add this line to the Makefile: MAKEFLAGS += –no-print-directory You can also set MAKEFLAGS in a makefile, to specify additional flags that should also be in…

  • WebDAV 101

    WebDAV (Web Distributed Authoring and Versioning) is an extension of the HTTP/1.1 protocol, which enables users to create, modify, and delete files on remote web servers. This protocol provides a standardized way for users to collaboratively edit and manage files on the web, much like a remote file system. WebDAV Features WebDAV introduces several features…

  • How to install NAS benchmark

    NAS benchmark link: http://www.nas.nasa.gov/publications/npb.html For Ubuntu, remember to install gfortran (sudo apt-get install gfortran) and change config/make.def to install NAS benchmark. Other distros are similar. Read more: Hadoop TeraSort Benchmark Big Data Benchmark from AMPLab of UC Berkeley A Simple Sort Benchmark on Hadoop PUMA: A MapReduce Benchmark Suite How to install ffmpeg on Linux…

  • Google PageRank Update 2011

    Update 3: Fclose.com sees minor PageRank updates on November 8, 2011. For example, some tooles and some posts get PageRank of 3 from 0. Update: Confirmed that Google updates the PageRank in toolbar on November 8 2011. Update 2: Google updates the PageRank in toolbar on August 05 2011. Update 1: Google updates the PageRank…

Leave a Reply

Your email address will not be published. Required fields are marked *