Skip to content

Instantly share code, notes, and snippets.

@CroModder
Last active July 28, 2016 12:50
Show Gist options
  • Save CroModder/f191a7b30dfca2711a9ed2391c37efa0 to your computer and use it in GitHub Desktop.
Save CroModder/f191a7b30dfca2711a9ed2391c37efa0 to your computer and use it in GitHub Desktop.
debug2Console
/**
* http://stackoverflow.com/questions/4323411/how-can-i-write-to-console-in-php#answer-37218530
* Simple helper to debug to the console
*
* @param $data object, array, string $data
* @param $context string Optional a description.
*
* @return string
*/
function debug_to_console( $data, $context = 'Debug in Console' ) {
// Buffering to solve problems frameworks, like header() in this and not a solid return.
ob_start();
$output = 'console.info( \'' . $context . ':\' );';
$output .= 'console.log(' . json_encode( $data ) . ');';
$output = sprintf( '<script>%s</script>', $output );
echo $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment