Error handling in php

Submitted by admin on Sun, 11/11/2018 - 14:25

# for checking backtrace of a function call

$e = new \Exception;
var_dump($e->getTraceAsString());

 

# for printing an array or object variable 

print_r($var);

 

# for showing a specific level of an array or object

/*
 * helper function for printing arrays and objects with max level
 *
 * Usage:
	$array = array_map(function ($v){
	    return parse($v, "...", 2);
	    }, $item);
    mdump($array);
 * */
function parse($v, $r, $max_d = 1, $d = 0) {
	if (is_array($v) || is_object($v)) {
		if ($d >= $max_d) {
			return $r;
		} else {
			$d++;
			return array_map(function ($v) use ($max_d, $r,  $d) {
				return parse($v, $r, $max_d, $d);
			}, $v);
		}
	} else {
		return $v;
	}
}