php debugger code example
Example 1: how to debug in php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
Example 2: php best debugging functions
<?php
$myVar = "hello world!";
var_dump($myVar);
print_r($myVar);
$allVars = get_defined_vars();
print_r($allVars);
debug_zval_dump($allVars);
function sayHello($hello) {
echo $hello;
debug_print_backtrace();
}
sayHello($myVar);
?>
Example 3: php best debugging functions
I find it very useful to print out to the browsers console instead of just var_dumping:
function console_log( $data ){
echo '<script>';
echo 'console.log('. json_encode( $data ) .')';
echo '</script>';
}
Usage:
$myvar = array(1,2,3);
console_log( $myvar );
Example 4: console_log in php
<?php
function console_log($output, $with_script_tags = true) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) .
');';
if ($with_script_tags) {
$js_code = '<script>' . $js_code . '</script>';
}
echo $js_code;
}
Example 5: debug php
$a = 123
echo ($a);
exit;
$array = array(
1 => 'apple',
2 => 'banana',
)
echo "<pre>";
print_r($array);
echo "</pre>";
echo "<pre>";
var_dump($array);
echo "</pre>";
dd($array)
pr ($array);
$json = {"a":"123"}
pr (json_encode($json));
exit;
print_r (json_encode($json));
exit;