Skip to content

Instantly share code, notes, and snippets.

@HynekS
Last active March 7, 2020 13:23
Show Gist options
  • Save HynekS/9d025dbaf8ee87607a2e0f0835399a9c to your computer and use it in GitHub Desktop.
Save HynekS/9d025dbaf8ee87607a2e0f0835399a9c to your computer and use it in GitHub Desktop.
Helper function to log php variables to a browser console.
<?php
// Tested on php 7.2.11 and chrome 80
function console_log(...$args)
{
$args_as_json = array_map(function ($item) {
return json_encode($item);
}, $args);
$js_code = "<script>console.log(
'%c 💬 log from PHP: ','background: #474A8A; color: #B0B3D6; line-height: 2',";
foreach ($args_as_json as $arg) {
$js_code .= "{$arg},";
}
$js_code .= ")</script>";
echo $js_code;
}
$list = ['foo', 'bar'];
$obj = new stdClass();
$obj->first_name = 'John';
$obj->last_name = 'Johnson';
echo console_log($list, 'Hello World', 123, $obj);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment