32

Does echo equal fputs( STDOUT ), or does echo write to a different stream? I've used PHP for a while now, but I don't know very well what's actually happening on a lower level.

bigblind
  • 12,539
  • 14
  • 68
  • 123

1 Answers1

49

According to PHP's manual page on wrappers, the answer is No.

php://output

php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print() and echo().

print and echo write to php://output stream, whereas fputs(STDOUT) writes to php://stdout.

I did a little test:

<?php

$output = fopen('php://output', 'w');
ob_start();

echo "regular echo\n";
fwrite(STDOUT, "writing to stdout directly\n");
fwrite($output, "writing to php://output directly\n");

$ob_contents = ob_get_clean();
print "ob_contents: $ob_contents\n";

This script outputs (tested on PHP 5.2.13, windows):

writing to stdout directly
ob_contents: regular echo
writing to php://output directly

i.e. writing to STDOUT directly bypasses ob handlers.

galymzhan
  • 5,505
  • 2
  • 29
  • 45
  • 1
    Where is the usual default place where the output for STDOUT is stored? E.g. STDERR writes to `C:\xampp\apache\logs\error.log` but STDOUT seems to go "missing". – Pacerier Oct 14 '14 at 07:13
  • STDOUT will store in /path/subdirectory/file-store.txt or it uses echo or print – Subhanshuja Nov 06 '20 at 11:28