A few debug functions based on a function I got off php.net (original function is included - it is from user notes on php.net)
By : Taoism
<?php
/**
* a variable to control whether debug code is displayed or not
*
* @param boolean $debug boolean to toggle debug code showing or not showing
*
*/
$debug=true;
//$debug=false;
/**
* simple debug function
*
* got this function from the user-feedback section for the
* print_r php function on www.php.net
* its very useful for debugging
*
* @param mixed $call a variable to be dumped for display
* @param string $cname a string to describe variable being dumped
*
*
*/
function dp($call,$cname) {
global $debug;
if($debug){
echo "<br/>".$cname.":<pre>";
if(!is_array($call)){$call=htmlspecialchars($call);}
print_r($call);
if(is_array($call)){reset($call);}
echo "</pre><hr/>\n";
}//end function dp() ======================
}//end dp
/**
* a function to display multiple variables side-by-side
*
* an extended version of dp()
* it takes an array in and dynamically creates variable names
* based on the array passed in so that side-by-side
* comparissons of variables can be done.
* this is most useful for comparing scopes...
* i.e. the $_POST scope to the $_SESSION scope...
* example of a call:
* dpa(array('xvar'=>'$xvar','AUTH'=>'$AUTH','tmp_AUTH_admin'=>'$tmp_AUTH_admin','x'=>'array_intersect($AUTH["xvar"],$xvar)'));
* Note that the key=>val pairs are VARNAME=>LABLE_TO_DESCRIBE.
* The varname has no $ so that it can be dynamically called within the function
*
* @author Keith Young
* @version 1.0
* @param array $dbug an array of key=>var pairs where the key is a variable name with no $, and the var is a lable to describe the key
* @global boolean $debug used to check if the function should run or not
* @global mixed $key this is a mixed data type based on the key value of the array passed in.
*
* @see dp()
*
*/
function dpa($dbug) {
global $debug;
echo '<br/>';
foreach($dbug as $key=>$val){global ${$key};}
if($debug){
$rspan=count($dbug);
($rspan==0)?$colpct=100:$colpct=100/$rspan;
echo '<table cellspacing="2" cellpadding="5" width="75%" style="background-color:black;">'."\n";
echo '<tr><th colspan="'.$rspan.'" style="background-color:lightgrey;font-size:11pt; font-family:"Verdana";font-weight:bold;color:black;"> D E B U G : </th></tr>'."\n";
echo '<tr>'."\n";
foreach($dbug as $key=>$val){
echo '<td width="'.$colpct.'%" valign="top" style="background-color:white;font-size:8pt; font-family:"Verdana";font-weight:normal;color:black;">'."\n";
echo '<span style="background-color:white;font-size:10pt; font-family:"Verdana";font-weight:bold;color:blue;">"'.$val.'":</span> <pre>'."\n";
if(!is_array(${$key})){${$key}=htmlspecialchars(${$key});}
print_r(${$key});
if(is_array(${$key})){reset(${$key});}
echo '</pre>'."\n";
echo '</td>'."\n";
}
echo '</tr></table><hr width="75%" align="left">'."\n";
}
}//end function dpa()
/**
* a function to display multiple variables side-by-side with the datatype of each variable displayed as well
*
* an extended version of dp()
* it takes an array in and dynamically creates variable names based on the array passed in so that side-by-side
* comparissons of variables can be done.
* this is most useful for comparing scopes...
* i.e. the $_POST scope to the $_SESSION scope...
* This function also displays the datatype of each variable
* example of a call:
* dpa(array('xvar'=>'$xvar','AUTH'=>'$AUTH','tmp_AUTH_admin'=>'$tmp_AUTH_admin','x'=>'array_intersect($AUTH["xvar"],$xvar)'));
* Note that the key=>val pairs are VARNAME=>LABLE_TO_DESCRIBE.
* The varname has no $ so that it can be dynamically called within the function
*
* @author Keith Young
* @version 1.0
* @param array $dbug an array of key=>var pairs where the key is a variable name with no $, and the var is a lable to describe the key
* @global boolean $debug used to check if the function should run or not
* @global mixed $key this is a mixed data type based on the key value of the array passed in.
*
* @see dp()
*
*/
function vdpa($dbug) {
global $debug;
echo '<br/>';
foreach($dbug as $key=>$val){global ${$key};}
if($debug){
$rspan=count($dbug);
($rspan==0)?$colpct=100:$colpct=100/$rspan;
echo '<table cellspacing="2" cellpadding="5" width="75%" style="background-color:black;">'."\n";
echo '<tr><th colspan="'.$rspan.'" style="background-color:lightgrey;font-size:11pt; font-family:"Verdana";font-weight:bold;color:black;"> D E B U G : </th></tr>'."\n";
echo '<tr>'."\n";
foreach($dbug as $key=>$val){
echo '<td width="'.$colpct.'%" valign="top" style="background-color:white;font-size:8pt; font-family:"Verdana";font-weight:normal;color:black;">'."\n";
echo '<span style="background-color:white;font-size:10pt; font-family:"Verdana";font-weight:bold;color:blue;">"'.$val.'":</span> <pre>'."\n";
if(!is_array(${$key})){${$key}=htmlspecialchars(${$key});}
var_dump(${$key});
if(is_array(${$key})){reset(${$key});}
echo '</pre>'."\n";
echo '</td>'."\n";
}
echo '</tr></table><hr width="75%" align="left">'."\n";
}
}//end function vdpa()
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo. FREE! Go There Now!
|
|
|
|
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
|
|
|
|
IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |