Skip to content

Instantly share code, notes, and snippets.

@AntonTrollback
Last active April 12, 2016 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonTrollback/8946d3959a6adf17ead4 to your computer and use it in GitHub Desktop.
Save AntonTrollback/8946d3959a6adf17ead4 to your computer and use it in GitHub Desktop.
log php to browser console
function logga() {
echo '<script>console.log(';
foreach (func_get_args() as $index => $arg) {
$type = gettype($arg);
if ($type === 'boolean') {
$arg = $arg ? 'true' : 'false';
} elseif ($type === 'string') {
$arg = '"' . $arg . '"';
} elseif ($type === 'array' || $type === 'object') {
$arg = json_encode($arg);
} elseif ($type === 'NULL' || $type === 'unknown type') {
$arg = 'null';
}
if ($index != 0) {
echo ', ';
}
echo $arg;
}
echo ')</script>';
}
// Usage
logga($something, $something_else);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment