Skip to content

Instantly share code, notes, and snippets.

@casperwilkes
Last active September 28, 2018 19:49
Show Gist options
  • Save casperwilkes/9042426 to your computer and use it in GitHub Desktop.
Save casperwilkes/9042426 to your computer and use it in GitHub Desktop.
Improved variable dumping. Lets you see the contents of a variable.
<?php
/**
* Lets you inspect variables. If no $var is defined, it will get all defined variables.
* @param mixed $var The variable to inspect.
* - Options:
* - __ALL__: Print all variables in global scope
* - __TRACE__: print out backtrace
* - $variable: Outputs variable info
* @param string $name An optional display name for variable.
* @param string $force Force an inspection type on the variable
* - options:
* - dump: var_dump
* - print: print_r
* - auto: let method decide
* @return void
*/
function see_var($var = '__ALL__', $name = '', $force = 'auto') {
$bt = debug_backtrace();
// Pre tags to force content into view //
echo '<pre>';
// Echo out custom name //
if ($name !== '') {
echo "Name: {$name}<br />";
}
// Echo out the file that function was called in //
echo "File: {$bt[0]['file']}<br />";
// echo out the line the function was called at
echo "Line: {$bt[0]['line']}<br />";
// Check for type of dump to preform //
if (is_string($var) && strtolower($var) == '__all__') {
// Variables in Global scope //
print_r($GLOBALS);
} elseif (is_string($var) && strtolower($var) == '__trace__') {
// Backtrace //
print_r($bt);
} elseif ($force === 'dump') {
// Force to dump //
var_dump($var);
} elseif ($force === 'print') {
// Force a print //
print_r($var);
} elseif (is_array($var)) {
// Depends on content //
print_r($var);
} else {
var_dump($var);
}
echo '</pre>';
}
/**
* Dump and die. Halts execution after dumping data.
* @param mixed $var Variable to dump [_ALL_, _TRACE_] are reserved.
* * _ALL_ dumps all variables in scope
* * _TRACE_ dumps a backtrace up until execution
* @param string $name
*/
function dd($var = '__ALL__', $name = '') {
see_var($var, $name, 'dump');
die(1);
}
/**
* Print and die. Halts execution after dumping data.
* @param mixed $var Variable to dump [_ALL_, _TRACE_] are reserved.
* * _ALL_ dumps all variables in scope
* * _TRACE_ dumps a backtrace up until execution
* @param string $name
*/
function pd($var = '__ALL__', $name = '') {
see_var($var, $name, 'print');
die(1);
}
/**
* Examples
*/
class Pet {
public $name;
public $species;
public $type;
public $birthday;
private $vet = 'N/A';
private $vaccinated = false;
public function set_vet($name = '') {
if (strlen($name)) {
$this->vet = $name;
$this->vaccinated = true;
}
}
public function get_records() {
return array('vet_name' => $this->vet, 'vaccinated' => $this->vaccinated);
}
}
$pup = new Pet();
$pup->name = 'Mutt';
$pup->type = 'Dog';
$pup->species = 'Boston Terrier';
$pup->birthday = '01.01.12';
$pets = $example = array('cat', 'dog' => $pup, 'fish' => 'goldfish', 'animals' => 3);
$string = 'Who doesn\'t love having a pet?';
// Array //
see_var($example, 'pets');
// Class //
see_var($pets['dog'], 'Dog');
// Force //
$pup->set_vet('Dr.Vetly');
see_var($pup->get_records(), 'Pup - print', 'print');
see_var($pup->get_records(), 'Pup - dump', 'dump');
// String //
see_var($string, 'Example String');
?>
// Output //
pets:
Array
(
[0] => cat
[dog] => Pet Object
(
[name] => Mutt
[species] => Boston Terrier
[type] => Dog
[birthday] => 01.01.12
[vet:Pet:private] => N/A
[vaccinated:Pet:private] =>
)
[fish] => goldfish
[animals] => 3
)
Dog:
object(Pet)[1]
public 'name' => string 'Mutt' (length=4)
public 'species' => string 'Boston Terrier' (length=14)
public 'type' => string 'Dog' (length=3)
public 'birthday' => string '01.01.12' (length=8)
private 'vet' => string 'N/A' (length=3)
private 'vaccinated' => boolean false
Pup - print:
Array
(
[vet_name] => Dr.Vetly
[vaccinated] => 1
)
Pup - dump:
array (size=2)
'vet_name' => string 'Dr.Vetly' (length=8)
'vaccinated' => boolean true
Example String:
string 'Who doesn't love having a pet?' (length=30)
@casperwilkes
Copy link
Author

This is really useful for a lot of situations. It will push content out of the way to display the contents of the variable; this is great for floating or stubborn divs. It's also good for showing the true nature of variables, like booleans. With print, booleans appear as '' or '1' but with dump, they will show as 'true' or 'false'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment