Write to the console in PHP

Last Updated On Saturday 6th Nov 2021

PHP Console log

Here’s the PHP helper function

php print console

	function debug_to_console($data) {
    $output = $data;
    if (is_array($output))
        $output = implode(',', $output);

    echo "<script>console.log('Debug Objects: " . $output . "' );</script>";

Use it like this

// Outputs like
Debug Objects: Test


  • json_encode() to check if the variable type is unnecessary and add a buffer to solve problems with frameworks.
	function debug_to_console($data, $context = 'Debug in Console') {

    // Buffering to solve problems frameworks, like header() in this and not a solid return.

    $output  = 'console.info(\'' . $context . ':\');';
    $output .= 'console.log(' . json_encode($data) . ');';
    $output  = sprintf('<script>%s</script>', $output);

    echo $output;
	$data = [ 'foo' => 'bar' ];

php print to console


  • 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.