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:

<span style="font-style: italic">&lt;?php</span>
<span style="font-style: italic">//##Simple Debugger Script##//</span>
<span style="font-style: italic">//By: Wardell Latham</span>
<span style="font-style: italic">//http://blog.wardelldesign.com/php/simple-php-debugger/&lt;/code&gt;</span>

<span style="font-style: italic">//##HANDLE ERRORS##//</span>
<span style="font-style: italic">//List of error codes and descriptions: http://php.net/manual/en/errorfunc.constants.php</span>
register_shutdown_function(<span style="font-style: italic">&#39;serious_error&#39;</span>);
set_error_handler(<span style="font-style: italic">&#39;debug_error&#39;</span>);
<span style="font-weight: bold">function</span> debug_error($type,$message,$file,$line){
debug(compact(<span style="font-style: italic">&#39;type&#39;</span>,<span style="font-style: italic">&#39;message&#39;</span>,<span style="font-style: italic">&#39;file&#39;</span>,<span style="font-style: italic">&#39;line&#39;</span>),<span style="font-style: italic">&quot;ERROR!&quot;</span>);
}
<span style="font-weight: bold">function</span> serious_error(){
<span style="font-weight: bold">if</span>(!is_null(error_get_last())){
$element=error_get_last();
<span style="font-style: italic">//print_r($element);</span>
$descriptor=<span style="font-style: italic">&quot;SERIOUS ERROR!!&quot;</span>;
debug($element,$descriptor);
}

}

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

Here are some example uses:

<span style="font-style: italic">&lt;?php</span> 
<span style="font-style: italic">//##Debug Function Examples##//</span>
<span style="font-weight: bold">include</span>(<span style="font-style: italic">&#39;debug.php&#39;</span>);
<span style="font-style: italic">//Test vars</span>
$random = <span style="font-weight: bold">array</span>(
<span style="font-style: italic">&quot;foo&quot;</span> =&amp;gt; <span style="font-style: italic">&quot;bar&quot;</span>,
<span style="font-style: italic">&quot;bar&quot;</span> =&amp;gt; <span style="font-style: italic">&quot;foo&quot;</span>,
);

$moreRandom = <span style="font-style: italic">&quot;wtf lmao&quot;</span>;
$myNum = 04;
$i=1;

debug($random,<span style="font-style: italic">&quot;THIS IS AN ARRAY&quot;</span>);
debug($moreRandom,<span style="font-style: italic">&quot;THIS IS A STRING&quot;</span>);
debug($myNum,<span style="font-style: italic">&quot;THIS IS A NUMBER&quot;</span>);
debug($random[<span style="font-style: italic">&#39;foo&#39;</span>],<span style="font-style: italic">&quot;THIS IS AN ARRAY VALUE&quot;</span>);

<span style="font-style: italic">//imaginaryFunction(); //trigger fatal error</span>

<span style="font-weight: bold">function</span> writesomething($mysomething){
<span style="font-weight: bold">if</span>(!isset($mysomething)||<span style="font-weight: bold">empty</span>($mysomething)){
$mysomething=<span style="font-style: italic">&quot;nothing to write&quot;</span>;
}
debug($mysomething,<span style="font-style: italic">&quot;THIS IS A FUNCTION PARAMETER&quot;</span>);
<span style="font-weight: bold">return</span> <span style="font-style: italic">&quot;nothing more to see here&quot;</span>;
}

trigger_error(<span style="font-style: italic">&quot;Some broken crap&quot;</span>); <span style="font-style: italic">//trigger user error</span>

writesomething(<span style="font-style: italic">&quot;debug from parent function&quot;</span>);
debug(writesomething(<span style="font-style: italic">&#39;&#39;</span>),<span style="font-style: italic">&quot;THIS IS A FUNCTION&quot;</span>);
debug(writesomething(<span style="font-style: italic">&#39;something written&#39;</span>),<span style="font-style: italic">&quot;THIS IS A FUNCTION&quot;</span>);

debug($i++);
debug($i++);
debug($i++);
<span style="font-style: italic">?&gt;</span>

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

THIS IS AN ARRAY : array (
&#39;foo&#39; =&amp;gt; &#39;bar&#39;,
&#39;bar&#39; =&amp;gt; &#39;foo&#39;,
)

THIS IS A STRING : &#39;wtf lmao&#39;

THIS IS A NUMBER : 4

THIS IS AN ARRAY VALUE : &#39;bar&#39;

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

THIS IS A FUNCTION PARAMETER : &#39;debug from parent function&#39;

THIS IS A FUNCTION PARAMETER : &#39;nothing to write&#39;

THIS IS A FUNCTION : &#39;nothing more to see here&#39;

THIS IS A FUNCTION PARAMETER : &#39;something written&#39;

THIS IS A FUNCTION : &#39;nothing more to see here&#39;

Line 35 : 1

Line 36 : 2

Line 37 : 3

debug.zip [download]

Comments are closed.

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