(07) 3040 7074

Simple PHP Debugger

By: , On Monday, May 21st, 2012

The following is a simple php script I created for debugging.

Features:

  • Easy to setup and use.
  • Outputs variable values to a text file.
  • Handles php errors.
  • Generates easy to read output.

Usage

To use the debugging function simply include the debugging script file at the top of your code, then pass the element you wish to debug as a parameter like so: debug($element);.

The function also takes three additional, optional parameters debug($element, $descriptor,$location,$mode); where $element is the variable you wish to debug, $descriptor is a user input string which defaults to the line debug was called if no value is given, $location is the output location of the text file created, possible values are ‘webroot’ and ‘currentdir’ the default value is ‘currentdir’, and mode is the file mode for writing to the output file which defaults to ‘a’ so data is appended to any existing data by default ‘w’ could be passed to the first debug call to remove any existing data from the text file ex. debug($element, ‘Element var value is’,'w’);.

Here is the debug script which can also be downloaded from the link below:

<?php
//##Simple Debugger Script##//
//By: Wardell Latham
//http://blog.wardelldesign.com/php/simple-php-debugger/</code>

//##HANDLE ERRORS##//
//List of error codes and descriptions: http://php.net/manual/en/errorfunc.constants.php
register_shutdown_function('serious_error');
set_error_handler('debug_error');
function debug_error($type,$message,$file,$line){
debug(compact('type','message','file','line'),"ERROR!");
}
function serious_error(){
if(!is_null(error_get_last())){
$element=error_get_last();
//print_r($element);
$descriptor="SERIOUS ERROR!!";
debug($element,$descriptor);
}

}

//##OUTPUT DEBUGGING MESSAGES##//
function debug($element='',$descriptor='',$location='currentdir',$mode='a'){
//return line if no descriptor is given
if($descriptor==''){
$descriptor=debug_backtrace();
$descriptor="Line ".$descriptor[0]['line'];
}
//set path for file output
$filePATH=$_SERVER['PHP_SELF'];
$theFILE=basename($_SERVER['PHP_SELF']);
$localROOT=$_SERVER['DOCUMENT_ROOT'];
$localFILE=$localROOT.$filePATH;
switch($location){
case 'webroot':
$localDIR = $localROOT."/";
break;
case 'currentdir':
$localDIR=str_replace($theFILE,"",$localFILE);
break;
default:
$localDIR=str_replace($theFILE,"",$localFILE);
}
//create file
$fileName ='debug.txt';
//file modes: a=append w=write (clears existing data) r=read only
if($newFile=fopen($localDIR.$fileName, $mode)){
//echo "The file WAS created!!!!
"; //debug message
}
//format input
$element = var_export($element, true);
//write to file
if(fwrite($newFile, $descriptor." : ".$element."rnrn")){
//echo "content written"; //debug message
fclose($newFile);
}else{
echo '
The file was NOT created!!!';
}
}
?>

Here are some example uses:

<?php 
//##Debug Function Examples##//
include('debug.php');
//Test vars
$random = array(
"foo" =&gt; "bar",
"bar" =&gt; "foo",
);

$moreRandom = "wtf lmao";
$myNum = 04;
$i=1;

debug($random,"THIS IS AN ARRAY");
debug($moreRandom,"THIS IS A STRING");
debug($myNum,"THIS IS A NUMBER");
debug($random['foo'],"THIS IS AN ARRAY VALUE");

//imaginaryFunction(); //trigger fatal error

function writesomething($mysomething){
if(!isset($mysomething)||empty($mysomething)){
$mysomething="nothing to write";
}
debug($mysomething,"THIS IS A FUNCTION PARAMETER");
return "nothing more to see here";
}

trigger_error("Some broken crap"); //trigger user error

writesomething("debug from parent function");
debug(writesomething(''),"THIS IS A FUNCTION");
debug(writesomething('something written'),"THIS IS A FUNCTION");

debug($i++);
debug($i++);
debug($i++);
?>

The example above should output the following to debug.txt :

THIS IS AN ARRAY : array (
'foo' =&gt; 'bar',
'bar' =&gt; 'foo',
)

THIS IS A STRING : 'wtf lmao'

THIS IS A NUMBER : 4

THIS IS AN ARRAY VALUE : 'bar'

ERROR! : array (
'type' =&gt; 1024,
'message' =&gt; 'Some broken crap',
'file' =&gt; 'C:\xampp\htdocs\debug\index.php',
'line' =&gt; 29,
)

THIS IS A FUNCTION PARAMETER : 'debug from parent function'

THIS IS A FUNCTION PARAMETER : 'nothing to write'

THIS IS A FUNCTION : 'nothing more to see here'

THIS IS A FUNCTION PARAMETER : 'something written'

THIS IS A FUNCTION : 'nothing more to see here'

Line 35 : 1

Line 36 : 2

Line 37 : 3

debug.zip [download]

Comments are closed.

© Wardell Design 2014
Entries (RSS) and Comments (RSS).