Miscellaneous Code

  Home arrow Miscellaneous Code arrow debugging function: rprint($variable)
MISCELLANEOUS CODE

debugging function: rprint($variable)
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-02-03

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    see http://sourceforge.net/projects/phpocketknife/ for more info regarding the license, and development info.

    By : timmyg

    <?
    /*
    rprint version 1.0.1 1/17/2002

    changelog
    version 1.0.1 1/17/2002
    Tim - Added a way to print the syntax of the function from code.
    pass in a variable named $PRINT_SYNTAX and the function will print
    the usage instructions on the webpage you're working on.

    version 1.0.0 1/15/2002
    released

    todo:
    cross-browser html code needed


    Developers who worked on this tool:
    Andrew Nelson - Initial concept, coding, and development.
    Tim Gallagher - minor change - see change log

    -----------------------------------------------------------------------------------------------------------------------------
    Copyright (C) 2002 by the PHPocket Knife development team:
    -------- Tim Gallagher
    -------- Andrew Nelson
    -----------------------------------------------------------------------------------------------------------------------------
    USE OF THIS SOFTWARE, CODE, OR ANY PORTION THEREOF IS BOUND BY THE TERMS AND CONDITIONS
    LISTED IN LICENSE.TXT, DISTRIBUTED WITH THIS FILE.
    -----------------------------------------------------------------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify it under the terms of
    the GNU General Public License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with this program;
    if not, write to
    Free Software Foundation, Inc.
    59 Temple Place, Suite 330
    Boston, MA 02111-1307 USA
    -----------------------------------------------------------------------------------------------------------------------------
    SEE LICENSE.TXT FOR FULL DETAILS
    -----------------------------------------------------------------------------------------------------------------------------

    documentation (of a sort)
    include the function in your file, or put it in your auto_prepend file

    when programming, to view the structure of a variable, simply pass it to rprint;
    example: rprint($variable)

    the html prints, and is returned by the function

    - timmyg

    */
    // ------------------------------------------------------------------------------------------------------------------------------------------------
    // replacement for print_r -------------------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------------------------------------------------


    define ("PRINT_SYNTAX", "xxx999PRINT_SYNTAX999xxx");

    function rprint($object,$expanded=FALSE,$advanced=TRUE,$border=TRUE,$first=TRUE,$level=0)
    {
    if ($object == $PRINT_SYNTAX)
    {
    echo "<pre>";
    ?>version 1.0.1 1/17/2002
    Copyright (C) 2002 by the PHPocket Knife development team
    rprint ($object=TRUE,$expanded=FALSE,$advanced=TRUE,$border=TRUE)
    default values:
    $object Always required
    $expanded: FALSE (setting this to true automatically expands all branches)
    $advanced: TRUE (setting to false displays a simpler structure)
    $border: TRUE (setting to false turns the outside border off of the advanced view)
    <?

    echo "</pre>";
    return NULL;
    }; // end if
    // $advanced=FALSE;
    if ($advanced)
    {
    $view_string = "[ view ]";
    $hide_string = "[ hide ]";
    }
    else
    {
    $view_string = "view";
    $hide_string = "hide";
    }; // end if

    if (!is_object($object) && !is_array($object))
    {
    $temp->ZqQ5ya = $object;
    return rprint($temp,$expanded,$advanced);
    }; // end if

    if ($level == 0)
    {
    ?>
    <style type="text/css"><!--
    .cell {font-family:verdana;border-style:solid;border-width:1px 3px 2px 0px;border-color:FFFFFF;};
    <?= (!$advanced ? "a {color:0000FF;text-decoration:none;font-weight:bold;};" : ""); ?>
    .subobject {border-width:1px 3px 1px 0px;};
    .topobject {border-top-width:3px;};
    .bottomobject {border-bottom-width:3px;};
    //--></style>
    <?
    }; // end if

    global $divid;
    $object_methods=array();
    $html="";
    $the_level = $level;
    $depth_limit = 7;

    if ($level>=$depth_limit)
    {
    $html='<table bgcolor="ffffff" cellspacing=0 cellpadding=0 border=0 style="' . (!$first ? 'padding:5px 15px 12px 9px;' : '') . '"><tr><td>';
    $html.='<table cellspacing=0 cellpadding=0 border='.($border?"0":"0").' style="border-style:solid;border-width:'.($advanced ? "2" : "0").'px;border-color:999999;">';
    if(count($object_var_names)==0)
    {
    $html.="<tr><td bgcolor=\"".$bgcolor."\" valign=top align=right style=\"font-size:".($advanced ? "10pt" : "8pt")."pt;padding:2px;\">&nbsp;( depth limit ".$depth_limit." )&nbsp;</td></tr>";
    };
    $html.='</table>';
    $html.='</td></tr></table>';
    return $html;
    }; // end if

    if ($first)
    {
    $html.='<table cellspacing="0" cellpadding="0" border="0">';
    }; // end if


    if($object===TRUE)
    {
    $object=$this;
    $object_vars=get_object_vars($this);
    $object_methods=get_class_methods(get_class($this));
    }
    else
    {
    if (is_array($object))
    {
    $object_vars=$object;
    }
    else
    {
    $object_vars=get_object_vars($object);
    $object_methods=get_class_methods(get_class($object));
    }; // end if
    };

    if ($first)
    {
    $html.='
    <script language="javascript"><!--

    function expand(what,who)
    {
    if (document.all[what].style.display=="" || document.all[what].style.display=="inline")
    {
    document.all[what].style.display="none";
    document.all[who].innerHTML="' . $view_string . '";
    }
    else
    {
    document.all[what].style.display="";
    document.all[who].innerHTML="' . $hide_string . '";
    }; // end if

    }; // end function

    //--></script>
    ';
    }; // end if

    $object_var_names=array_keys($object_vars);
    //$object_method_names=array_keys($object_methods);
    sort($object_var_names);
    sort($object_methods);


    $html.='<table bgcolor="ffffff" cellspacing=0 cellpadding=0 border=0 style="' . (!$first ? 'padding:5px 15px 12px 9px;' : '') . '"><tr><td>';


    $html.='<table cellspacing=0 cellpadding=0 border='.($border?"0":"0").' style="border-style:solid;border-width:'.($advanced ? "2" : "0").'px;border-color:999999;">';

    if(count($object_var_names)==0 && count($object_methods)==0)
    {
    $html.="<tr><td bgcolor=\"FFFFFF\" valign=top align=right style=\"font-size:".($advanced ? "10pt" : "8pt").";padding:2px;\">&nbsp;( empty )&nbsp;</td></tr>";
    };

    if ($object)
    {
    $parent = get_parent_class($object);
    $html.="<tr><td class=\"cell\" colspan=\"3\" bgcolor=\"999999\" valign=top align=left style=\"border-width:3px 3px 1px 3px;color:FFFFFF;font-size:".($advanced ? "10pt" : "8pt").";padding:2px;\">&nbsp;" .
    "<b>" . get_class($object) . "</b>" .
    ($parent != "" ? " extends <b>" . $parent . "</b>" : "") .
    "&nbsp;</td></tr>";
    }; // end if


    for ($a = 0; $a < count($object_var_names) ; $a++)
    {
    global $rprint_track;
    $html.='<tr>';
    //$bgcolor=($border?($a%2?"EEEEEE":"FFFFFF"):($a%2?"EEEEFF":"CCCCFF"));

    $var=$object_vars[$object_var_names[$a]];

    switch(strtolower(gettype($var)))
    {
    case "array": $bgcolor="DDDDFF"; $line_bgcolor="AAAACC"; break;
    case "boolean": $bgcolor="FFFFCC"; $line_bgcolor="CCCC99"; break;
    case "string": $bgcolor="FFDD99"; $line_bgcolor="CCAA66"; break;
    case "object": $bgcolor="FFCCFF"; $line_bgcolor="CC99CC"; break;
    case "integer": $bgcolor="CCFFFF"; $line_bgcolor="AAAACC"; break;
    case "double": $bgcolor="99EEEE"; $line_bgcolor="66BBBB"; break;
    case "resource": $bgcolor="CC99CC"; $line_bgcolor="996699"; break;
    case "unknown type": $bgcolor="FFCCCC"; $line_bgcolor="CC9999"; break;
    case "null": $bgcolor="EEEEEE"; $line_bgcolor="BBBBBB"; break;
    default: $bgcolor="FFFFFF"; $line_bgcolor="000000"; break;
    };
    if (!$advanced)
    {
    $bgcolor="FFFFFF";
    }; // end if

    $sub = "";
    if (gettype($var) == "array" || gettype($var) == "object")
    {
    $sub .= " subobject";
    }; // end if
    if ($a==0)
    {
    $sub .= " topobject";
    }; // end if
    if ($a==count($object_var_names)-1)
    {
    $sub .= " bottomobject";
    }; // end if
    if ($object_var_names[$a] !== "ZqQ5ya")
    {
    $html.="<td width=\"0%\" bgcolor=\"".$bgcolor."\" class=\"cell".$sub."\" valign=top align=".($advanced ? "right" : "left")." style=\"".(!$advanced ? "border-style:solid;border-color:FFFFFF FFFFFF FFFFFF ".$line_bgcolor.";border-width:3px 0px 3px 8px;" : "border-left-width:3px;")."font-size:".($advanced ? "10pt" : "8pt").";\"><nobr>&nbsp;<span style=\"".($advanced ? '' : 'font-size:10pt;').'font-weight:bold;">'.$object_var_names[$a]."</span>&nbsp;</nobr>".($advanced ?"</td>":"");
    }; // end if
    $html.=($advanced ? "<td width=\"0%\" bgcolor=\"".$bgcolor."\" class=\"cell".$sub."\" valign=top align=center style=\"font-size:".($advanced ? "8pt" : "8pt").";\"><nobr>&nbsp;" : "").($advanced ? "" : "(").(gettype($var)=="object" ? (get_class($var) != "stdClass" ? get_class($var). " " : "") : "") . gettype($var).($advanced ? "" : ")")."&nbsp;</nobr>".($advanced ? "</td>" :"");
    $html.=($advanced ? "<td width=\"100%\" bgcolor=\"".$bgcolor."\" class=\"cell".$sub."\" valign=top style=\"font-size:".($advanced ? "10pt" : "8pt").";\">" : (gettype($var) != "array" && gettype($var) != "object" ? "=" : ""));
    switch(gettype($var))
    {
    case "array":
    $rprint_track[$the_level] = (object) NULL;

    if ($the_level > 0) { $rprint_track[$the_level-1]->arrays++; }; // end if
    $subobject = rprint($var,$expanded,$advanced,($border?FALSE:TRUE),FALSE,$level+1);
    $divid++;
    $html.='&nbsp;<a id="debug_link_'.$divid.'" href="javascript:expand(\'debug_div_'.$divid.'\',\'debug_link_'.$divid.'\');">'.($expanded ? $hide_string : $view_string).'</a>' .
    ($advanced ? '&nbsp; &nbsp; ' . count($var) . ' elements &nbsp; ' .
    ($rprint_track[$the_level]->strings > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->strings . ' string' . ($rprint_track[$the_level]->strings > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->booleans > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->booleans . ' boolean' . ($rprint_track[$the_level]->booleans > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->arrays > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->arrays . ' array' . ($rprint_track[$the_level]->arrays > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->integers > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->integers . ' integer' . ($rprint_track[$the_level]->integers > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->doubles > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->doubles . ' double' . ($rprint_track[$the_level]->doubles > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->resources > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->resources . ' resource' . ($rprint_track[$the_level]->resources > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->unknowns > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->unknowns . ' unknown' . ($rprint_track[$the_level]->unknowns > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->nulls > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->nulls . ' null' . ($rprint_track[$the_level]->nulls > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->objects > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->objects . ' object' . ($rprint_track[$the_level]->objects > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->constructors > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->constructors . ' constructor' . ($rprint_track[$the_level]->constructors > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->methods > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->methods . ' method' . ($rprint_track[$the_level]->methods > 1 ? 's' : '') : '' ) .
    ' &nbsp;' : '');
    $html.='</td><tr><td class="cell" bgcolor="'.$bgcolor.'" colspan="3" id="debug_div_'.$divid.'" style="display:'.($expanded?'inline':'none').';padding-left:'.($advanced ? '15' : '20').'px;border-top-width:2px;border-left-width:3px;">';
    $html.=$subobject;
    break;
    case "boolean":
    if ($the_level > 0) { $rprint_track[$the_level-1]->booleans++; }; // end if
    $html.="<nobr>&nbsp;";
    $html.=($var==TRUE?"TRUE":"FALSE");
    $html.="&nbsp;</nobr>";
    break;
    case "string":
    if ($the_level > 0) { $rprint_track[$the_level-1]->strings++; }; // end if
    /*
    if (preg_match("/password/",$object_var_names[$a]))
    {
    $var=str_repeat("*",strlen($var));
    }; // end if
    */
    $html.="&nbsp;<nobr>".nl2br(htmlspecialchars($var))."</nobr>&nbsp;";
    break;
    case "object":
    $rprint_track[$the_level] = (object) NULL;
    if ($the_level > 0) { $rprint_track[$the_level-1]->objects++; }; // end if

    $sub_object = rprint($var,$expanded,$advanced,($border?FALSE:TRUE),FALSE,$the_level+1);
    $divid++;
    $html.='&nbsp;<a id="debug_link_'.$divid.'" href="javascript:expand(\'debug_div_'.$divid.'\',\'debug_link_'.$divid.'\');">'.($expanded ? $hide_string : $view_string).'</a>' .
    ($advanced ? '&nbsp ' .
    ($rprint_track[$the_level]->strings > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->strings . ' string' . ($rprint_track[$the_level]->strings > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->booleans > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->booleans . ' boolean' . ($rprint_track[$the_level]->booleans > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->arrays > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->arrays . ' array' . ($rprint_track[$the_level]->arrays > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->integers > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->integers . ' integer' . ($rprint_track[$the_level]->integers > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->doubles > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->doubles . ' double' . ($rprint_track[$the_level]->doubles > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->resources > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->resources . ' resource' . ($rprint_track[$the_level]->resources > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->unknowns > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->unknowns . ' unknown' . ($rprint_track[$the_level]->unknowns > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->nulls > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->nulls . ' null' . ($rprint_track[$the_level]->nulls > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->objects > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->objects . ' object' . ($rprint_track[$the_level]->objects > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->constructors > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->constructors . ' constructor' . ($rprint_track[$the_level]->constructors > 1 ? 's' : '') : '' ) .
    ($rprint_track[$the_level]->methods > 0 ? ' &nbsp; ' . $rprint_track[$the_level]->methods . ' method' . ($rprint_track[$the_level]->methods > 1 ? 's' : '') : '' ) .
    ' &nbsp;' : '');
    $html.='</td><tr><td class="cell" bgcolor="'.$bgcolor.'" colspan="3" id="debug_div_'.$divid.'" style="display:'.($expanded?'inline':'none').';padding-left:15px;border-top-width:2px;border-left-width:3px;">';
    $html.=$sub_object;
    break;
    case "integer":
    if ($the_level > 0) { $rprint_track[$the_level-1]->integers++; }; // end if
    $html.="<nobr>&nbsp;";
    $html.=htmlspecialchars($var);
    $html.="&nbsp;</nobr>";
    break;
    case "double":
    if ($the_level > 0) { $rprint_track[$the_level-1]->doubles++; }; // end if
    $html.="<nobr>&nbsp;";
    $html.=htmlspecialchars($var);
    $html.="&nbsp;</nobr>";
    break;
    case "resource":
    if ($the_level > 0) { $rprint_track[$the_level-1]->resources++; }; // end if
    $html.="<nobr>&nbsp;";
    $html.=htmlspecialchars($var);
    $html.="&nbsp;</nobr>";
    break;
    case "unknown type":
    if ($the_level > 0) { $rprint_track[$the_level-1]->unknowns++; }; // end if
    $html.="<nobr>&nbsp;";
    $html.=htmlspecialchars($var);
    $html.="&nbsp;</nobr>";
    break;
    case "NULL":
    if ($the_level > 0) { $rprint_track[$the_level-1]->nulls++; }; // end if
    $html.="<nobr>&nbsp;";
    $html.="&nbsp;</nobr>";
    break;
    };
    $html.="</td>";
    $html.='</tr>'."\n";
    }; // end for loop

    $object_name=get_class($object);
    if ($the_level > 0)
    {
    $rprint_track[$the_level-1]->methods=count($object_methods);
    }; // end if
    for ($a = 0; $a < count($object_methods) ; $a++)
    {
    $bgcolor="99FF99";
    $line_bgcolor = "66CC66";
    $type="method";
    if($object_name==$object_methods[$a])
    {
    $bgcolor="00FF00";
    $line_bgcolor = "00CC00";
    $type="constructor";
    if ($the_level > 0)
    {
    $rprint_track[$the_level-1]->constructors++;
    $rprint_track[$the_level-1]->methods--;
    }; // end if
    }; // end if
    if (!$advanced)
    {
    $bgcolor = "FFFFFF";
    }; // end if
    $html.='<tr>';
    $html.="<td width=\"0%\" class=\"cell\" bgcolor=\"".$bgcolor."\" valign=top align=".($advanced ? "right" : "left")." style=\"".(!$advanced ? "border-style:solid;border-color:FFFFFF FFFFFF FFFFFF ".$line_bgcolor.";border-width:3px 0px 3px 8px;" : "border-left-width:3px;")."font-weight:bold;font-size:".($advanced ? "10pt" : "8pt").";\"><nobr>&nbsp;".$object_methods[$a]."&nbsp;</nobr>".($advanced ? "</td>" : "");
    $html.=($advanced ? "<td width=\"0%\" class=\"cell\" bgcolor=\"".$bgcolor."\" valign=top align=center style=\"font-size:".($advanced ? "10pt" : "8pt").";\">":"")."<nobr>".($advanced ? "&nbsp;" : "(").$type.($advanced ? "" : ")")."&nbsp;</nobr>".($advanced ? "</td>" : "");
    $html.=($advanced ? "<td width=\"100%\" class=\"cell\" bgcolor=\"".$bgcolor."\" valign=top align=left style=\"font-size:".($advanced ? "10pt" : "8pt").";\">":"")."<nobr>&nbsp;";
    $html.='&nbsp;</nobr></td>';
    $html.='</tr>';
    };

    $html.='</table>';

    $html.='</td></tr></table>';

    if ($level == 0)
    {
    echo $html;
    }; // end if

    return $html;
    } // end function
    ?>
    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

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap