29

In javascript, I can print to the debug console using

console.log("Message here");

I'm now writing a php script, and would like to print to the debug console. Using the above code doesn't work in php. It seems I need to use either echo or some other command but I need the output to appear inside the output console, not the browser window.

5
  • 4
    I don't know of a way to echo to the console but you certainly can write to an error log which would be best. php.net/manual/en/function.error-log.php
    – Squeegy
    Commented May 20, 2015 at 18:37
  • 4
    not possible. php runs on the server and cannot "talk" to your browser's JS debug console. at best PHP could do something like echo "<script>console.log('hello, world');</script>", but that's not php "logging", that's php outputting JS code that triggers the logging.
    – Marc B
    Commented May 20, 2015 at 18:38
  • 1
    can you give example for what you want to print there? Commented May 20, 2015 at 18:38
  • 2
    PHP lives on the server side and logging is usually done using log files on the server. The browser console is for client side code (i.e. JavaScript). If you really want to debug some PHP app, you should probably get in touch with Xdebug or xhprof
    – feeela
    Commented May 20, 2015 at 18:46
  • 1
    @Squeegy: Thanks for pointing out that I should be using php's error log for this instead of outputting to the console.
    – OB7
    Commented May 20, 2015 at 19:06

7 Answers 7

47
<?php
   echo '<script>console.log("Your stuff here")</script>';
?>
5
  • 6
    Works! But in my case I need to echo a variable, so the code looks like this: echo "'<script>console.log(\"$value\")</script>'";
    – OB7
    Commented May 20, 2015 at 18:44
  • 1
    How about array data? Commented Sep 22, 2016 at 10:08
  • 8
    echo '<script>console.log('.$variable.')</script>'; is the best way to print out variables I think. Commented Mar 21, 2017 at 18:51
  • 2
    @blackandorangecat echo '<script>console.log('.json_encode($variable).')</script>';
    – Brad Kent
    Commented Oct 3, 2018 at 19:52
  • @RobbiNespu json_encode() the var.. if you want to use console.table(),, you'll need to jump quite a few hoops... or use a library such as github.com/bkdotcom/PHPDebugConsole
    – Brad Kent
    Commented Oct 3, 2018 at 19:55
18

This will work with either an array, an object or a variable and also escapes the special characters that may break your JS :

function debugToConsole($msg) { 
        echo "<script>console.log(".json_encode($msg).")</script>";
}

Edit : Added json_encode to the echo statement. This will prevent your script from breaking if there are quotes in your $msg variable.

1
  • Re: comments to accepted answer, wrapping $VAR with json_encode($VAR) was necessary for preventing quotes within the variable from breaking the dump to console.log(). E.g.: echo '<script>console.log('.json_encode($charge).')</script>'; Commented Jul 7, 2023 at 3:59
1
<?php  echo "<script>console.log({$yourVariable})</script>"; ?>
0
0

You can also try this way:

<?php
   echo "<script>console.log('$variableName')</script>";
?>
2
  • But then $variableName cannot contain quotes... you must escape them first. check other answers Commented May 23, 2017 at 14:12
  • 1
    @AbrahamMurcianoBenzadon escape via json_encode()... will support, strings, arrays, bool, null....
    – Brad Kent
    Commented Oct 3, 2018 at 19:57
0

There are much better ways to print variable's value in PHP. One of them is to use buildin var_dump() function. If you want to use var_dump(), I would also suggest to install Xdebug (from https://xdebug.org) since it generates much more readable printouts.

The idea of printing values to browser console is somewhat bizarre, but if you really want to use it, there is very useful Google Chrome extension, PHP Console, which should satisfy all your needs. You can find it at consle.com It works well also in Vivaldi and in Opera (though you will need "Download Chrome Extension" extension to install it). The extension is accompanied by PHP library you use in your code.

0

https://github.com/bkdotcom/PHPDebugConsole

Support for all the javascript console methods:
assert, clear, count, error, group, groupCollapsed, groupEnd, info, log, table, trace, time, timeEnd, warn
plus a few more:
alert, groupSummary, groupUncollapse, timeGet

$debug = new \bdk\Debug(array(
    'collect' => true,
    'output' => true,
    'outputAs' => 'script',
));

$debug->log('hello world');
$debug->info('all of the javascript console methods are supported');
\bdk\Debug::_log('can use static methods');
$debug->trace();
$list = array(
    array('userId'=>1, 'name'=>'Bob', 'sex'=>'M', 'naughty'=>false),
    array('userId'=>10, 'naughty'=>true, 'name'=>'Sally', 'extracol' => 'yes', 'sex'=>'F'),
    array('userId'=>2, 'name'=>'Fred', 'sex'=>'M', 'naughty'=>false),
);
$debug->table('people', $list);

this will output the appropriate <script> tag upon script shutdown

alternatively, you can output as html, chromeLogger, FirePHP, file, plaintext, websockets, etc

upcomming release includes a psr-3 (logger) implementation

0

For something simple that work for arrays , strings , and objects I builed this function:

function console_testing($var){

    $var = json_encode($var,JSON_UNESCAPED_UNICODE);

    $output = <<<EOT
    <script>
        console.log($var); 
    </script>
EOT;

    echo $output;

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.