#native_company# #native_desc#
#native_cta#

dump

By Daniel Jaenecke
on July 26, 2002

Version: 0.3

Type: Function

Category: Other

License: GNU General Public License

Description: Dump content of a variable of any kind ( array, string, numeric, object )

<?

/*
 * string dump ( mixed $var );
 * 
 * will generate a HTML-table-representation of the given 
 * variable
 *
 * (c) 2001-2002 Daniel Jaenecke <jaenecke[at]gmx[dot]li>
 */

function dump ( $var ) {

/*
 * patterns for output
 * customize these to change reperesentation of data
 */
	$pattern_key = '<td><b>%s</b></td>';
	$pattern_type = '<td><i>%s</i></td>';
	$pattern_value = '<td>%s</td>';

/*
 *  handling non-arrays
 */
	if ( !is_array ( $var ) ) {
	
		switch ( gettype ( $var ) ) {

			case 'string':
				if ( empty ( $var ) ) {
				
					return '&nbsp;';
					
				}
				else {
				
					return sprintf ( '<code>%s</code>', htmlentities ( $var ) );
				
				}
				
				break; // string
				
			case 'boolean':
				if ( $var ) {
				
					return '<i>true</i>';
					
				}
				else {
				
					return '<i>false</i>';
					
				}
				
				break; // boolean
				
			case 'object':
				return dump ( array (
					'class'			=>	get_class ( $var ),
					'parent_class'	=>	get_parent_class ( $var ),
					'methods'		=>	get_class_methods ( get_class ( $var ) ),
					'attributes'	=>	get_object_vars ( $var )
					) );
				
				break; // object

			case 'resource':
				return sprintf ( '%s (%s)', $var, get_resource_type ( $var ) );
				
				break; // resource
				
			default:
				return $var;
				
				break; // default
		
		} // switch gettype ( value )
	
	} // !is_array
	
/*
 * generate output
 */
	$out = '<table cellspacing="0" cellpadding="1" border="1">';
	
	foreach ( $var as $key => $value ) {
	
	// get type of current value
		$type = substr ( gettype ( $var[ $key ] ), 0, 3 );
		
	// determine size of value if available
		if ( $type == 'arr' ) 
			$type .= sprintf ( '(%s)', sizeof ( $var[ $key ] ) );
		elseif ( $type == 'str' )
			$type .= sprintf ( '(%s)', strlen ( $var[ $key ] ) );

		$out .= sprintf ( 
			'<tr>' . 
				$pattern_key .
				$pattern_type .
				$pattern_value .
			'</tr>',
			$key, $type, dump ( $value )
		);

	} // foreach
	
	$out .= '</table>';
	
	return $out;

} // function dump ()

?>