Skip to content

Instantly share code, notes, and snippets.

@pauloiankoski
Last active July 22, 2022 13:21
Show Gist options
  • Save pauloiankoski/4b92e66e653a77e05a3d8a6ec4f38bdb to your computer and use it in GitHub Desktop.
Save pauloiankoski/4b92e66e653a77e05a3d8a6ec4f38bdb to your computer and use it in GitHub Desktop.
console.log() PHP variables
<?php
if ( ! function_exists( 'console_log' ) ) {
function console_log( $var ) {
$backtrace = debug_backtrace();
$log[] = sprintf(
'"DEBUG: On %s at line %d:"',
str_replace( $_SERVER['DOCUMENT_ROOT'], '', $backtrace[0]['file'] ),
$backtrace[0]['line']
);
$log[] = sprintf( '"(%s)"', gettype( $var ) );
$log[] = json_encode( $var );
printf( '<script class="console-log-debug">console.log(%s);</script>', implode( ', ', $log ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment