Printing / Echoing To Console from PHP Script

Viewed 39026

I'm debugging a PHP script that runs a couple SQL queries and emails a set of users. I'm sure this is a very basic thing, but every time I try to echo, print, or print_r it doesn't appear while running the script.

So say I have this in the script:

print("This should print");
echo "on the command line";

When I run the script via command line php script.php it doesn't actually print anything to the command line while running the script.

Is there a way of having PHP print to console? I feel like I'm missing something extremely basic here.

4 Answers

A good approach could be:

function log($message)
{
    $message = date("H:i:s") . " - $message - ".PHP_EOL;
    print($message);
    flush();
    ob_flush();
}

It will output to your terminal each line you call. Just use :

log("Something");
log("Another line");

The following code working fine for me.

<?php
    print("This should print");
    echo "on the command line";
?>

with tags.

Related