How to Write to the Console in PHP

How can I write to the console in PHP?

Firefox

On Firefox you can use an extension called FirePHP which enables the logging and dumping of information from your PHP applications to the console. This is an addon to the awesome web development extension Firebug.

  • http://www.studytrails.com/blog/using-firephp-in-firefox-to-debug-php/

Chrome

However if you are using Chrome there is a PHP debugging tool called Chrome Logger or webug (webug has problems with the order of logs).

More recently Clockwork is in active development which extends the Developer Tools by adding a new panel to provide useful debugging and profiling information. It provides out of the box support for Laravel 4 and Slim 2 and support can be added via its extensible API.

Using Xdebug

A better way to debug your PHP would be via Xdebug. Most browsers provide helper extensions to help you pass the required cookie/query string to initialize the debugging process.

  • Chrome - Xdebug Helper
  • Firefox - The easiest Xdebug
  • Opera - Xdebug
  • Safari - Xdebug Toggler

How to echo (or print) to the js console with php


<?php
echo '<script>console.log("Your stuff here")</script>';
?>

How to print in console with PHP?

Unlike Javascript, which runs in a virtual machine in your browser, PHP runs on the remote machine. Unless you take pains to set up remote debugging, PHP does not have any secret channel to your workstation where you are coding. JetBrains has a nice IDE and offers documentation on how to set up this secret communication channel so you can step through code. It's fairly involved.

Eclipse PDT is what I use and it also has a fairly elaborate setup.

If you just want to write messages that you can inspect to check values and stuff--something like console.log, I suggest that you write a logging function that writes to a file. Something like this:

function my_log($msg) {
// consider changing this log file's location to something else
if (!file_put_contents("/tmp/my-log-file.txt", "[" . date("Y-m-d H:i:s") . "] " . $msg . "\n", FILE_APPEND)) {
die("Unable to write log file!");
}
}

Note that if you are running your PHP scripts via a web server, then the web server user will need permission to write the file. On Debian/Ubuntu machines, the apache user is www-data. On Red Hat/CentOs machines, I think it is apache. You can find out which user the web server runs as by putting this script on your server and loading it in your browser:

<?php
passthru("whoami");

My instructions here assume you are working on a *nix machine. If you're running windows, we might have to dig a little deeper.

How do I write to the console from a Laravel Controller?

Aha!

This can be done with the following PHP function:

error_log('Some message here.');

Found the answer here: Print something in PHP built-in web server

Print or Display PHP array in Browser Console or in Javascript

Try using json_encode()

Example

echo "<script>console.log('" . json_encode($data) . "');</script>";

How to print to console from a php file in wordpress

If you are thinking about the javascript console, you can not do this from PHP.

You have a few options you could choose from:

  • echo
  • var_dump
  • create a log file
  • xdebug

For a quick check for a variables value I would use var_dump, it will also show you the data type of the variable. This will be output to the browser when you request the page.

Printing / Echoing To Console from PHP Script

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



Related Topics



Leave a reply



Submit