Skip to content

Instantly share code, notes, and snippets.

@robertopc
Last active February 19, 2016 17:08
Show Gist options
  • Save robertopc/43bcf4d28a32bc95956f to your computer and use it in GitHub Desktop.
Save robertopc/43bcf4d28a32bc95956f to your computer and use it in GitHub Desktop.
print_r_pre, var_dump_pre, print_r_console, var_dump_console
<?php
/*
* Date: 17/03/2014
* Author : RobertoPC
* Author URI : http://www.robertopc.com.br
* Gist URI : https://gist.github.com/robertopc/43bcf4d28a32bc95956f
* Description: Functions to debug code in PHP
* Functions : print_r_pre, var_dump_pre, print_r_console, var_dump_console
* Version : 1.0
* License : MIT
*/
// verify if the function was declared for prevent errors
if( ! function_exists('print_r_pre') ){
// print_r of the php into <pre> tags
function print_r_pre( $data ) {
print'<pre>print_r_pre &gt;<br><br>';
print_r( $data );
print'<br>&lt; print_r_pre</pre>';
}
// end print_r_pre
}
// verify if the function was declared for prevent errors
if( ! function_exists('var_dump_pre') ) {
// var_dump of the php into <pre> tags
function var_dump_pre( $data ) {
print'<pre>var_dump_pre &gt;<br><br>';
var_dump( $data );
print'<br>&lt; var_dump_pre</pre>';
}
// end var_dump_pre
}
// verify if the function was declared for prevent errors
if( ! function_exists('print_r_console') ) {
// print_r of the php in the browser console
function print_r_console( $data ) {
// begin console.log
print"<script>";
print"console.log('print_r_console >\\n\\n'+\n";
// get print_r string
$print_r_string = print_r( $data, true );
// split lines by line-break
$print_r_string_split = preg_split( "/\n|\r\n|\r/", $print_r_string );
foreach( $print_r_string_split as $row ) {
// print lines
print "'". addslashes( $row ) ."\\n'+";
}
// end console.log
print"'< print_r_console');</script>";
}
// end print_r_console
}
// verify if the function was declared for prevent errors
if( ! function_exists('var_dump_console') ) {
// var_dump of the php in the browser console
function var_dump_console( $data ) {
// begin console.log
print"<script>";
print"console.log('var_dump_console >\\n\\n'+\n";
// start capture from output buffer
ob_start();
var_dump( $data );
// get var_dump string from buffer
$var_dump_string = ob_get_contents();
ob_end_clean();
// split lines by line-break
$var_dump_string_split = preg_split( "/\n|\r\n|\r/", $var_dump_string );
foreach( $var_dump_string_split as $row ) {
// print lines
print "'". addslashes( $row ) ."\\n'+";
}
// end console.log
print"'< var_dump_console');</script>";
}
// end var_dump_console
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment