Last active
March 7, 2020 13:23
-
-
Save HynekS/9d025dbaf8ee87607a2e0f0835399a9c to your computer and use it in GitHub Desktop.
Helper function to log php variables to a browser console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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