debugging in php 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: debug php

// ------ string -------
// use echo, exit for output
$a = 123
echo ($a);
exit;


// ------ array -------
$array = array(
	1 => 'apple',
  	2 => 'banana',
)

// Method 1
echo "<pre>";
print_r($array);
echo "</pre>";

// Method 2 
echo "<pre>";
var_dump($array);
echo "</pre>";

// Method 3 (no need exit) beacause of dd is already exit
dd($array)
  
// Method 4 (cakephp)
pr ($array);


// ------ json -------
$json = {"a":"123"}

pr (json_encode($json));
exit;

print_r (json_encode($json));
exit;

Example 4: Best debugging tools for php

Xdebug, PHP DebugBar , MacGDBp, Kint, Whoops, Krumo, Pinba, Gubed PHP Debugger, DBG, and PHP_Dyn are some best debugging tools for php.

Tags:

Misc Example