Last active
August 29, 2015 13:56
-
-
Save NoMan2000/8938921 to your computer and use it in GitHub Desktop.
A debug function that tells you variable name, line number, arrays, and prints to the javascript console or echos out directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The Joe Debug function. To use successfully, create a global variable called $MSDebugVar. | |
// It accepts an optional string calls $testVals that can be used to pass in any values that | |
// You wish to be tested during debugging. | |
// Refactor to remove the isarray and just use the built-in php isarray | |
function joeDebug($testVals = null, $isJavaScript = false, $isArray = false){ | |
if(!function_exists('print_var_name')){ | |
function print_var_name($var) { | |
foreach($GLOBALS as $var_name => $value) { | |
if ($value === $var) { | |
return $var_name; | |
}// End if value | |
} //End foreach | |
}// End if function exists check. | |
return false; // no value | |
} // end function print_var_name | |
global $MSDebugVar; | |
$backtrace = debug_backtrace(); | |
$last = $backtrace[0]; | |
$echoLine = "{$last['function']}() "; | |
$echoLine .= "line {$last['line']}."; | |
$echoPosition = "I am at position {$MSDebugVar}. "; | |
if(!$isJavaScript){ | |
$echoLine .= "called from {$last['file']}"; | |
echo $echoLine . "\r\n"; | |
echo $echoPosition . "\r\n"; | |
if($testVals && !$isArray){ | |
$varname = print_var_name($testVals); | |
echo "The variable name is " . $varname . "\r\n"; | |
echo "The variable tested has returned " . $testVals . "\r\n"; | |
} | |
else if($testVals && $isArray){ | |
$output = explode("\n", print_r($testVals, true)); | |
foreach ($output as $line) { | |
if (trim($line)) { | |
$line = addslashes($line); | |
echo "{$line}"; | |
} //End if Trimline | |
} //End foreach | |
}else{ | |
// This is the $testVals and @array | |
} | |
}else{ | |
$echoLine = trim(htmlspecialchars($echoLine)); | |
$echoPosition = trim(htmlspecialchars($echoPosition)); | |
$echoFull = $echoLine; | |
$echoFull .= $echoPosition; | |
if($testVals && !$isArray){ | |
$testVals = trim(htmlspecialchars($testVals)); | |
$varname = print_var_name($testVals); | |
$varname = trim(htmlspecialchars($varname)); | |
$echoFull .= "The variable name is " . $varname . " "; | |
$echoFull .= 'values returned are ' . $testVals . " "; | |
} | |
if($testVals && $isArray){ | |
$output = explode("\n", print_r($testVals, true)); | |
foreach ($output as $line) { | |
if (trim($line)) { | |
$line = addslashes($line); | |
echo "<script type='text/javascript'>"; | |
echo "console.log(\"{$line}\");"; | |
echo "</script>"; | |
} | |
} | |
} | |
?> | |
<script type='text/javascript'>console.log('<?php echo $echoFull; ?> ');</script> | |
<?php | |
} //End else | |
$MSDebugVar++; | |
return $MSDebugVar; | |
} // End function. | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment