11

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.

5
  • 3
    That code should work just fine assuming there is no code executed earlier that closes stdout or redirects it e.g. to a file or enables an output buffer. Commented Dec 5, 2013 at 14:23
  • php -c index.php php.net/manual/en/install.windows.commandline.php Commented Dec 5, 2013 at 14:25
  • Possible duplicate of stackoverflow.com/questions/4323411/php-write-to-console Commented Dec 5, 2013 at 14:25
  • @ThiefMaster It would seem that printing variables isn't handled the way I'm used to. Printing strings work fine, whoops! Commented Dec 5, 2013 at 14:27
  • As recommended by answers, flush() could solve your problem but there is a simpler solution: just add a new line character ("\n") at the end of your messages. It is the magic that automatically triggers a flushing of the console. Commented Jul 29, 2018 at 21:56

5 Answers 5

5

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");
Sign up to request clarification or add additional context in comments.

Comments

3

Thats the method of doing it.

Things to check for are output buffering http://php.net/manual/en/function.ob-flush.php

Is that code actually being run ? Make sure it doesnt branch before it gets there

2 Comments

It would seem that printing basic strings does work, it's the variables I'm printing that is doing it. :-X
Yep, print_r() seems to do it. :P Since he deleted his comment: print_r("Printing Variable: " . $varName );
3

I know it is old, but I am going post my own solution to this just in case if someone would run into similar situation. I had a problem that a legacy command line PHP script wouldn't print or echo anything to the terminal when DSN is incorrectly configured and the script got stuck for very long time (not sure how long, never waited for it to terminate by itself). After putting ob_end_flush() in the entry line of the script, the output came back to the terminal. So it turned out that all output was buffered and since the script stuck at some point, the output stayed buffered and so never went to the terminal.

Comments

2

The following code working fine for me.

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

with tags.

1 Comment

It was an issue with how I was printing variables. Should probably run test code before SO'ing it haha.
0

updated 2023

if you using a php server like this local php server

php -S 0.0.0.0:80

then you can't see any errors in the ClI, like in a Linux terminal or Windows cli.

You can see that in the html document.

If you use PHP inside the JS, then you can print it into the browser console.

 $message = "Thanks.. This is log";
echo "<script>console.log('".$message."');</script>";

this sample example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.