Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing / Echoing To Console from PHP Script

Tags:

php

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.

like image 690
AlbertEngelB Avatar asked Dec 05 '13 14:12

AlbertEngelB


People also ask

Can you print to console in PHP?

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.

How do you type echo in PHP?

The PHP echo Statement The echo statement can be used with or without parentheses: echo or echo() .

Can we use print instead of echo in PHP?

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.

Can you console log from PHP?

The PHP functions echo and var_dump can be used to console log and monitor data on a web page.


2 Answers

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

like image 139
exussum Avatar answered Oct 04 '22 21:10

exussum


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");
like image 44
Tiago Gouvêa Avatar answered Oct 04 '22 20:10

Tiago Gouvêa