Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Created October 2, 2013 14:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtsternberg/6794786 to your computer and use it in GitHub Desktop.
Save jtsternberg/6794786 to your computer and use it in GitHub Desktop.
A `print_r` or `var_dump` replacement that expands the variable into valid PHP code. (for copy/pasting back into the project, etc)
<?php
/**
* A `print_r` or `var_dump` replacement that expands the variable into valid PHP code.
* @uses var_export
* @param array|object $array_or_object $variable to expand/view
* @param boolean $echo Whether to return or display the data
* @return string Variable representation that is valid PHP code.
*/
function expand_code( $array_or_object, $echo = true ) {
$expanded = '<xmp>$expanded = '. str_ireplace( 'stdClass::__set_state', '(object) ', var_export( $array_or_object, true ) ) .'</xmp>';
if ( $echo )
echo $expanded;
else
return $expanded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment