PHP Classes

dump_r: Display values of variables

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 73%Total: 809 All time: 4,258 This week: 455Up
Version License PHP version Categories
dump_r 1.0.4MIT/X Consortium ...5.3PHP 5, Debug
Description 

Author

This class can display values of variables similarly to the PHP var_dump and print_r functions.

It can show values of scalar variables or arrays or objects formatted in a HTML page using colors to denote different types of values.

Objects and arrays can be traversed recursively up to a given depth level.

XML and JSON strings can be displayed as a listing of contained values.

Values that contain objects or arrays may collapse or expand when the user clicks on the values.

Innovation Award
PHP Programming Innovation award nominee
October 2012
Number 2
PHP has several functions that can display the contents of variables, including complex data structures such as nested arrays or objects.

This class provides a more sophisticated solution that not only can display the data structures on a Web page, but it also shows buttons that you can use to fold and unfold certain data members.

The resulting user interface makes it easier to read and browse complex data structures with many nested member.

Manuel Lemos
Picture of Leon Sorokin
Name: Leon Sorokin <contact>
Classes: 1 package by
Country: United States United States
Innovation award
Innovation award
Nominee: 1x

 

Details

dump_r() ======== a cleaner, leaner mix of `print_r()` and `var_dump()` _(MIT Licensed)_ ![screenshot](https://github.com/leeoniya/dump_r.php/raw/master/test/dump_r.png) ### Demo: http://o-0.me/dump_r/ ### Installing __Composer__ https://packagist.org/packages/leeoniya/dump-r ```json { "require": { "leeoniya/dump-r": "dev-master" } } ``` __Require__ ```php require 'dump_r.php'; ``` ### Using & Config Use `dump_r()` as a drop-in replacement for `print_r()` and `var_dump()`. It has some additional arguments that control output. The full signature of the function is: ```php function dump_r($value, $return = false, $html = true, $depth = 1e3, $expand = 1e3); ``` - `$value` is the thing you want to dump - `$return` determines whether to return the dump rather than output to screen - `$html` controls whether the output is text or html - `$depth` sets the recursion limit for the dump - `$expand` sets the auto-expanded child node depth There are also two modifier keys that can be used to control how the node expanding/collapsing works: 1. Holding `Shift` while toggling will expand/collapse the full depth of the node. 2. Hold `Ctrl` while toggling will expand/collapse all siblings after that node also. This is useful if you have an array of objects/arrays and want to expand all of them to one level simultaneously by clicking just the first one in the group. It works well for deep, complex objects. 3. `Shift` and `Ctrl` can be used together. Double-clicking binary strings will toggle them between mixed hex/ascii and hex-only representations: ![binary_toggle](https://github.com/leeoniya/dump_r.php/raw/master/test/binary_toggle.gif) Some types of strings can be pretty-printed and additonal rendering options can be tweaked (shown with defaults): ```php dump_r\Rend::$xml_pretty = false; // pretty-print xml strings dump_r\Rend::$json_pretty = false; // pretty-print json strings dump_r\Rend::$sql_pretty = false; // pretty-print sql strings (requires https://github.com/jdorn/sql-formatter) dump_r\Rend::$recset_tbls = true; // recordset detection & table-style output dump_r\Rend::$val_space = 4; // number of spaces between key and value columns (affects text output only, not html) ``` Circular reference (recursion) detection and duplicate output is indicated like this for arrays, objects, closures and resources respectively: `[*]`,`{*}`,`(*)`,`<*>`. You can re-style all aspects of the html output using CSS, everything is class-tagged. ### Extending Adding your own classifiers & parsers is extremely easy. Here are instructions and two concrete examples of how the `String` base type can be subtyped. First for displaying EXIF data of `jpeg` and `tiff` image paths and then showing row data from CSV file paths. This array ```php $stuff = [ 'imgs/img_1771.jpg', 'data/people.csv', ]; ``` Which would normally dump like this: ![coretyped](https://github.com/leeoniya/dump_r.php/raw/master/test/coretyped.png) Can be dumped like this with subtyping: ![usertyped](https://github.com/leeoniya/dump_r.php/raw/master/test/usertyped.png) To do this, hook the correct core type and provide a function that classifies and processes the raw value, then modifies and returns an instance of `Type`. Here are the properties that can be modified/augmented: 1. `$type->types` - Array of subtype string(s) of your choice. These get appended as CSS classes and are also displayed inline. 2. `$type->nodes` - Array of expandable subnodes to display. Provide `null` if no subnodes are needed or to retain any subnodes extracted by the core type. 3. `$type->length` - A string to be displayed at the end of the line, indicating length of subnodes. You can also abuse this param to display other length-esque information (the EXIF example below uses it to display image dimensions inline). Provide `null` to retain the default length display for the hooked core type. ```php use dump_r\Type; // Example 1: dump EXIF data with image filepath strings Type::hook('String', function($raw, Type $type, $path) { // match path-esque strings (containing '/' or '\') trailed by an // EXIF-capable image extension, then verify this file actually exists if (preg_match('#[\/]+.+\.(jpe?g|tiff?)$#', $raw) && is_file($raw)) { $nodes = $exif = exif_read_data($raw, 0, true); $len = $exif['COMPUTED']['Width'] . 'x' . $exif['COMPUTED']['Height']; $type->types = ['image']; $type->nodes = ['EXIF' => $nodes['EXIF']]; $type->length = $len; return $type; } }); // Example 2: dump CSV records with csv filepath strings Type::hook('String', function($raw, Type $type, $path) { if (preg_match('#[\/]+.+\.csv$#', $raw) && is_file($raw)) { $type->types = ['csv']; $type->nodes = csv2array($raw); $type->length = count($type->nodes); return $type; } }); function csv2array($file) { $csv = []; $rows = array_map('str_getcsv', file($file)); $header = array_shift($rows); foreach ($rows as $row) $csv[] = array_combine($header, $row); return $csv; } ``` All core types (see `src/dump_r/Node` dir) can be hooked by their fully namespaced names. For example, if you wanted to further subtype a JSON object string, you would use ```php Type::hook('String\\JSON\\Object', function($raw, Type $type, $path) { // code here }); ``` ### Filtering, Marking & Recursion Control Using the same `Type` hooks (introduced above) allows you to modify additional aspects of the renderer and iterator. **Skip specific nodes based on their properties or path in the hierarchy** ```php // prevent anything keyd under 'xxx' from dumping Type::hook('*', function($raw, Type $type, $path) { if (end($path) === 'xxx') return false; }); ``` **Stop recursion of specific nodes** ```php // prevent arrays keyed under 'c' from dumping sub-nodes Type::hook('Array0', function($raw, Type $type, $path) { if (end($path) === 'c') $type->depth = 1; return $type; }); ``` **CSS-tag nodes via classes** ```php // tag nodes keyed under `yyy` with addl CSS classes Type::hook('*', function($raw, Type $type, $path) { if (end($path) === 'yyy') { $type->classes[] = 'marked'; } return $type; }); ```

  Files folder image Files (36)  
File Role Description
Files folder imagelib (1 file)
Files folder imagesrc (2 directories)
Files folder imagetest (7 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Plain text file dump_r.php Class main class file
Accessible without login Plain text file README.md Doc. description

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:809
This week:0
All time:4,258
This week:455Up
 User Ratings  
 
 All time
Utility:87%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:79%StarStarStarStar
Examples:79%StarStarStarStar
Tests:58%StarStarStar
Videos:-
Overall:73%StarStarStarStar
Rank:117