Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PHP Dev Helpers
<?php
if (PHP_SAPI === 'cli' && function_exists('xdebug_disable')) {
xdebug_disable();
}
if (PHP_SAPI === 'cli' && strpos($_SERVER['_'], 'bin/phpunit') !== false) {
error_reporting(E_ERROR);
}
/**
* A few helper functions for debugging PHP
* See: http://top-frog.com/2011/09/27/a-few-php-dev-helper-functions/ for info
*/
function pp() {
$msg = __v_build_message(func_get_args());
$id = 'pp-' . mt_rand();
register_shutdown_function('__v_output', $msg, $id);
}
function dp() {
$msg = __v_build_message(func_get_args(), 'var_dump');
$id = 'dp-' . mt_rand();
register_shutdown_function('__v_output', $msg, $id);
}
function ep() {
$msg = __v_build_message(func_get_args());
$msg_array = explode("\n", $msg);
foreach ($msg_array as $line) {
error_log('**: '.str_replace("\t", ' ', $line));
}
}
function __v_output($msg, $id) {
$style = 'white-space: pre; text-align: left; margin: 10px 2%; ' .
'font: normal normal 11px/1.4 menlo, monaco, monospaced; ' .
'background: white; color: black; padding: 8px; ' .
'letter-spacing: normal; word-spacing: normal; ' .
'box-shadow: 0px 0px 3px rgba(187, 187, 187, 0.5);';
$js = '<script type="text/javascript"> ' .
'var _body = document.getElementsByTagName("body")[0]; ' .
'_body.insertBefore(document.getElementById("%s"), _body.firstChild); ' .
'</script>';
echo '<pre id="' . $id . '" style="' . $style . '">' . htmlspecialchars($msg, ENT_COMPAT, 'UTF-8') . '</pre>' .
PHP_EOL . sprintf($js, $id) . PHP_EOL;
}
function __v_build_message($vars, $func = 'print_r', $sep = ', ') {
$msgs = array();
if (!empty($vars)) {
foreach ($vars as $var) {
if (is_bool($var)) {
$msgs[] = ($var ? 'true' : 'false');
}
elseif (is_scalar($var)) {
$msgs[] = $var;
}
else {
switch ($func) {
case 'print_r':
case 'var_export':
$msgs[] = $func($var, true);
break;
case 'var_dump':
ob_start();
var_dump($var);
$msgs[] = ob_get_clean();
break;
}
}
}
}
return implode($sep, $msgs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment