Categories
PHP

PHP Debug Message Function

PHP coders frequently need to display a message in code for debugging purposes. It helps to be able to display these messages in a distinctive matter that sets them apart from normal page content, and to display the line number where the message was triggered.

This function displays the massage in a visually distinctive form, and prepends the line number from which the function is called.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function debugMsg($message) {
	/*-------------------------------------------------------------------------------------------------
	 *   SCRIPT: debugMsg()
	 *  PURPOSE: Accept a text string and display is in a formatted div, preceeded by the line
	 *           number of the line that called the message.
	 *   SYNTAX: debugMsg('this is my error message.');
	 *-----------------------------------------------------------------------------------------------*/
	// 	
	// determine calling line - line number which called this function	
	// Reference: http://php.net/manual/en/function.debug-backtrace.php
	$call_info = array_shift( debug_backtrace() );
	$calling_line = $call_info['line'];
	// now print the message with the line number from which the message was generated.
	echo "<div style='border:1px dashed silver;background-color:#F7F3E8;margin:.25em; padding:.25em .5em;color:darkgray;font-size:100%;'>";
	echo "<pre style='margin:0px;'>$calling_line: $message</pre>";
	echo "</div>";
}