PHP: List of functions to help you Debug php

How to..

Include once the debug.php before your codes.

eg include_once(‘debug.php’)

1. pr($var1, $var2, $arr1, $arr2 so on… ); //print_r declared variables
2. pe($var1, $var2, $arr1, $arr2 so on… ); //print_r declared variables and EXIT
3. opr($obj); //print_r properties of an object
4. ope($obj); //print_r properties of an object and EXIT
5. btr(); //print_r backtrace
5. bte(); //print_r backtrace and EXIT
6. _db($var1, $var2, $arr1, $arr2 so on… ) // for some reasons you want to save the variable’s value to database.

mysql codes

CREATE TABLE `debug` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`key` MEDIUMTEXT NULL,
	`value` MEDIUMTEXT NULL,
	`created` DATETIME NULL DEFAULT NULL,
	`modified` DATETIME NULL DEFAULT NULL,
	PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB

You can download HERE the complete codes.

<?php

function pr(){
	$numargs = func_num_args();
	$arg_list = func_get_args();
        echo '<div style="border-bottom:1px solid gray;">';
	echo '<pre style="padding:10px;font-size:12px;color:gray;background:black;margin:0px;">';
	if ($numargs==1){
		print_r ($arg_list[0]);
	}
	else{
		print_r ($arg_list);
	}
	echo '</pre>';
        echo '</div>';
}

function pe(){
	$numargs = func_num_args();
	$arg_list = func_get_args();
        echo '<div style="border-bottom:1px solid gray;">';
	echo '<pre style="padding:10px;font-size:12px;color:gray;background:black;margin:0px;">';
	if ($numargs==1){
		print_r ($arg_list[0]);
	}
	else{
		print_r ($arg_list);
	}
	echo '</pre>';
        echo '</div>';
	die();
}
function oDetails($obj=null){
    $data = null;
    if (is_object($obj)){
        $data['name'] = get_class($obj);
        $data['methods'] = get_class_methods($obj);
    }
    else{
        $data['name'] = 'variable not OBJECT';
    }
    return $data;
}
function opr($obj=null){
    pr(oDetails($obj));
}
function ope($obj=null){
    pe(oDetails($obj));
}
function gTxt(){
    $arrTxt = array();
    $arrTxt[] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed facilisis lorem nunc, eu congue massa blandit ac. Fusce vehicula dignissim lacus, sit amet ullamcorper diam tincidunt a. Nulla rutrum blandit purus, nec posuere ligula consequat in. Vestibulum rhoncus ultricies est, eget posuere dolor interdum accumsan. Morbi consectetur elit nec vulputate pharetra. Aliquam ultricies libero et ornare rutrum. Duis eros enim, viverra vel elit eget, mattis mattis dolor. Quisque a scelerisque ligula. Fusce ut vulputate nisl. Fusce at pulvinar justo, nec varius libero. Donec rutrum dolor a sodales molestie. Nullam imperdiet felis vitae pharetra ornare.';
    $arrTxt[] = 'Nunc eu mi porta, sollicitudin magna a, molestie dolor. Pellentesque placerat purus massa, a volutpat orci vehicula et. Ut quis est euismod, pharetra dolor quis, posuere libero. Donec pharetra varius pellentesque. In et lobortis sapien. Donec eget aliquet arcu. Nullam ornare dui ipsum. Ut aliquet dictum erat vel vehicula. Proin fermentum porta velit a placerat. Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
    $arrTxt[] = 'Quisque nec velit dignissim, accumsan odio id, pretium erat. Maecenas commodo orci ac ligula rutrum dictum. Cras tellus metus, pharetra sit amet enim eu, pulvinar mollis sapien. In posuere vitae elit at varius. Sed ac lacinia felis, accumsan cursus urna. Quisque tellus neque, consectetur in dui sit amet, facilisis adipiscing nulla. Mauris volutpat massa sit amet sapien ultricies placerat. In at porta purus.';
    $arrTxt[] = 'Nulla facilisi. Curabitur ornare, risus vitae mattis elementum, nulla tortor aliquet turpis, at fermentum orci sem vitae est. Ut varius urna non lorem eleifend, ut consectetur lacus dignissim. Etiam feugiat erat ac convallis semper. Phasellus posuere ipsum id cursus luctus. Ut gravida adipiscing sapien. Suspendisse consequat augue sapien, vitae commodo massa hendrerit et. Cras venenatis velit eget ligula viverra, id rhoncus magna accumsan. Sed congue justo non dolor scelerisque venenatis. Pellentesque quis vulputate risus. Suspendisse ut auctor eros.';
    $arrTxt[] = 'Phasellus sit amet sem sapien. Ut sed tempor dui. Ut feugiat at ante quis consequat. Aliquam pellentesque, felis eget vulputate varius, enim felis tincidunt leo, vel elementum libero orci in quam. Nullam faucibus ante sem, tempus tempor est varius vel. Suspendisse aliquet, augue vel rhoncus ultrices, nisi dui congue lectus, id aliquet eros nulla eget est. Morbi mollis arcu vel eros elementum, sit amet lobortis sapien hendrerit. Cras ultricies sed tortor ac eleifend. Quisque dapibus sapien at leo ultrices accumsan. Morbi cursus leo in fermentum blandit. Aenean congue id massa et blandit.';
	
	$rnd = rand(3,10);
	$allStr = '';
	for ($x=0; $x<$rnd; ++$x){
		$allStr .= $arrTxt[ rand(0,4) ].'<br /><br />';
	}
	
    return $allStr;
}
function gNum(){
    return rand(0,1000);
}

function bt(){
    echo '<div style="border-bottom:1px solid gray;">';
    echo '<pre style="padding:10px;font-size:12px;color:green;background:black;margin:0px;">';
    debug_print_backtrace();
    echo '</pre>';
    echo '</div>';
}
function btr(){
    bt();
}
function bte(){
    bt();die();
}

function _db(){
	$oDb = new DbModel();
	$oDb->connect();
	$oDb->table('debug');
	$arg_list = func_get_args();
	$currentDateTime = date('Y-m-d H:i:s');
	foreach ($arg_list as $k => $v){
		if (is_array($v)){
			foreach ($v as $k2 => $v2){
				$oDb->insert(array(
					'key' => $k2,
					'value' => (is_array($v2))?'==>'.json_encode($v2):$v2,
					'created' => $currentDateTime,
					'modified' => $currentDateTime
				));
			}
		}
		else{
			$oDb->insert(array(
				'key' => $k,
				'value' => (is_array($v))?'==>'.json_encode($v):$v,
				'created' => $currentDateTime,
				'modified' => $currentDateTime
			));
		}
	}
}

class DbModel{
	private $table;
	private $mysqliConnect;
	function connect(){
		$host = "localhost";
		$user = "root";
		$pass = "";
		$dbname = "DBNAME";
		// 1. Create a database connection
		$this->mysqliConnect = mysqli_connect($host,$user,$pass);
		if (!$this->mysqliConnect) {
			die("Database connection failed: " . mysqli_error());
		}

		// 2. Select a database to use 
		$db_select = mysqli_select_db($this->mysqliConnect, $dbname);
		if (!$db_select) {
			die("Database selection failed: " . mysqli_error());
		}
	}
	
	function table($arg = null){
		$this->table = $arg;
		return $this;
	}
	
	function insert($args = array()){
		$field = '`'.implode('`,`', array_keys($args)).'`';
		$values = "'" . implode("','", $args) . "'";
		mysqli_query($this->mysqliConnect, "INSERT INTO ".$this->table." (".$field.") VALUES (".$values.")");
	}
	
	function delete($args = array()){
		$str = '';
		foreach ($args as $k => $v){
			$str .= $k . "'" . $v ."'";
		}
		mysql_query("DELETE FROM ".$this->table." WHERE ". $str);
	}
	
	function edit($cond = null, $args = array()){
		$str = '';
		foreach ($args as $k => $v){
			$str .= $k . "'" . $v ."',";
		}
		if (!empty($cond)){
			$cond = ' WHERE '.$cond;
		}
		mysql_query("UPDATE ".$this->table." SET ". $cond);
		/*
		UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
*/
	}
	
}
?>

Tags:

Comments are closed.