Skip to content

Instantly share code, notes, and snippets.

@s4wny
Last active August 29, 2015 14:18
Show Gist options
  • Save s4wny/b54b148cd05a76cdd79f to your computer and use it in GitHub Desktop.
Save s4wny/b54b148cd05a76cdd79f to your computer and use it in GitHub Desktop.
Debug function for PHP
<?php
// Source: http://stackoverflow.com/a/19788805/996028
// Author: Mark Baker
/*
* Skriver ut i det här formatet:
* *namnet på $var*:
* var_dump
*
* Så $hej = 1; Debug($hej);
* $hej:
* 1
*/
function Debug($var) {
$backtrace = debug_backtrace()[0];
$fh = fopen($backtrace['file'], 'r');
$line = 0;
while (++$line <= $backtrace['line']) {
$code = fgets($fh);
}
fclose($fh);
preg_match('/' . __FUNCTION__ . '\s*\((.*)\)\s*;/u', $code, $name);
echo '<pre>'. trim($name[1]) .":\n";
var_export($var);
echo '</pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment