lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PHP: trigger_notice()
A little function I put together -- I find it handy for debugging. It would be even handier if it had a shorter name:

/* Fx trigger_notice()
******************************/
function trigger_notice($notice='default', $headline=FALSE, $add_style=TRUE)
{
// *** DATA

# style settings
$color1 = '#336699';
$bg_color = '#fdfefe';
$text_color = $color1;
$border = "2px solid $color1";
$font = "'Courier New',courier,monospace";
$padding = '.4em';
$margin = '.2em .4em';
$border_width = '2px 0';

# define inline style for output
$style = " style=\"background-color:$bg_color; color:$text_color; "
. "font-family:$font; padding:$padding; margin:$margin; border:$border; "
. "border-width:$border_width; clear:both; text-align:left;\"";

# output
$_DIV['head'] = '';
$_DIV['notice'] = '';
$_DIV['foot'] = '';

# return
$html = '';


// *** MANIPULATE

# backtrace vars
$_TRACE = debug_backtrace();
$_file = $_TRACE[0]['file'];
$_line = $_TRACE[0]['line'];

# build headline
if ( strlen($headline) )
{
$_DIV['head'] = strtoupper($headline);
}
else
{
$_DIV['head'] = 'NOTICE';
}

# build footer string
$_info_string = "File: $_file <br />Line: $_line";
$_DIV['foot'] = '<div style="margin:.2em 0; color:green; font-family:monospace; '
. 'font-size:10px; font-weight:bold;">' . $_info_string . '</div>';

# build notice
if ( $notice == 'default' )
{
$_fx_trace = '';
foreach ( $_TRACE as $_level )
{
$_fx_trace .= "fx call: {$_level['function']} on line {$_level['line']} of file {$_level['file']}\n";
}
$_DIV['notice'] = $_fx_trace;
$text_color = '#ccc';
}
# array notice
elseif ( is_array($notice) )
{
$_DIV['notice'] = print_r($notice, TRUE);
}
else
{
$_DIV['notice'] = $notice;
}

# style
if ( $add_style )
{
# define inline style for output
$style = " style=\"background-color:$bg_color; color:$text_color; "
. "font-family:$font; padding:$padding; margin:$margin; border:$border; "
. "border-width:$border_width; clear:both; text-align:left;\"";
}

# build html
$html = <<<NOTICE

<!-- NOTICE -->
<div class="trigger_notice"{$style}>
<h4 style="margin:.4em 0 0">{$_DIV['head']}</h4>
{$_DIV['foot']}
<pre>{$_DIV['notice']}</pre>
</div>
<!-- end NOTICE -->

NOTICE;

// *** RETURN

# echo
echo $html;

# return
return TRUE;

} /* end Fx */
/*****************************/


You can just drop it in your code anywhere you want to see the current state of a variable or array.

Output:

NOTICE

File: c:\path\to\your_file.php
Line: 84
fx call: trigger_notice on line 84 of file c:\path\to\your_file.php