Personalised PHP debugging
Published: 13 years ago
- Snippet Length: 10 minutes
- Knowledge Required: PHP, HTML
- Difficulty: Basic-Intermediate
There are many situations where you find yourself fixing errors in php pages, a great way to fix it quickly would be to debug your code logically and methodically.
One way to do this is to build your own debugging and error display function
An Explanation
For this I will be using several inbuilt magic constants, global variables and some of php's inbuilt functions to make a simple function output a great deal of useful information.
- __FILE__ - retrieves the full path and file name
- __LINE__ - retrieves the line number of your current php script
- $_SERVER['REMOTE_ADDR'] - is an array containing information such as headers, paths, and script locations.
- gettype()
- print_r - Print the message in a readable format, exceptionally helpful for arrays and objects
Writing the function!
First step is to create the function and it's parameters
// declare function function debug( $file_name, $line_no, $data = '', $my_ip = '' ) { // check if the person debugging matches the SERVERs recorded IP address if ( $_SERVER['REMOTE_ADDR'] == $my_ip ) { } }
The statement above also checks if the soon to be passed IP address of you the developer so that nobody else will see this, if your currently sharing an internet connection with hundreds of people with the same IP it maybe wise to add an additional argument in the if statement
$file_name parameter will be required by default and will normally be __FILE__ unless you specify this manually
$line_no will be required by default and will usually be __LINE__
$data parameter will be used to pass data that wants to be examied i.e checking a variable exists or the contents of an array
$my_ip is not required by default, if it isn't set then the debugging information will be viewable by everyone, if it is set then it has to match the value of $_SERVER['REMOTE_ADDR'] ( This feature exists so you can limit the amount of people viewing the debugging information to just your IP)
Put some meat on the bones!
function debug( $file_name, $line_no, $data = '', $my_ip = '' ) { // if IP is set, check there is a match if ( $my_ip != '' && $_SERVER['REMOTE_ADDR'] != $my_ip ) { return; } // add some css to the div we will output $css = 'position: absolute; top:20px; left: 20px; z-index: 10; '; $css .= 'width:800px; padding:6px; border:2px solid red; line-spacing:24px; '; $css .= 'font-family:Helvetica; background: #fff; font-size:12px;'; // capture data type to aid debugging $data_type = gettype( $data ); // build debugging message $debug = '<div style=" '.$css.' ">'; $debug .= 'File Name: '.$file_name.'
'; $debug .= 'Line Number: '.$line_no.'
'; $debug .= 'Data Type: '.$data_type.'
'; $debug .= '<p>Print Data:'; // if user passes any data then display it if ( $data ) { $debug .= '</p>'; $debug .= ''; $debug .= print_r( $data, true); $debug .= ''; } else { $debug .= 'data passed is NULL'; } $debug .= '</p>'; $debug .= '</div>'; // print the debugging information print $debug; } // close function
Calling our debug() function
You can include your debug function either in a OOP manor or put it inside the file your working with, this is why we pass the variables __FILE__ and __LINE__ because if the function is stored inside a different location it will not return the information we want.
$data = array(1 => 'one', 2 => 'two', 3 => 'three'); debug( __FILE__ , __LINE__ , $data, 'your.ip.address.here eg: 8.8.8.8' )