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.
The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console.
The PHP echo Statement The echo statement can be used with or without parentheses: echo or echo() .
In PHP, the Print statement is also used to show the output. We can use it as an alternative to Echo. However, it is slower than Echo and returns an integer value 1.
The PHP functions echo and var_dump can be used to console log and monitor data on a web page.
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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With