Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@soubhikchatterjee
Created January 21, 2017 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soubhikchatterjee/98fdcb027db34454778d8f8682a4e606 to your computer and use it in GitHub Desktop.
Save soubhikchatterjee/98fdcb027db34454778d8f8682a4e606 to your computer and use it in GitHub Desktop.
Quick Debugging Functions for your PHP Application
/**
* Print with formatting and Die. Used for debugging purpose.
*
* @author Soubhik Chatterjee
* @param string $text
* @param string $label
*/
function prd($text = '', $label = ''){
echo '<pre>',"\n";
echo '==', $label, '==', "\n";
print_r($text);
echo "\n",'</pre>', "\n\n";
exit;
}
/**
* Short function for print_r() with formatting. Used for debugging purpose.
*
* @author Soubhik Chatterjee
* @param string $text
* @param string $label
*/
function pr($text, $label = ''){
echo '<pre>',"\n";
echo '==', $label, '==', "\n";
print_r($text);
echo "\n",'</pre>', "\n\n";
}
/**
* Prints all specified arguments with their variable name
*
* @author Soubhik Chatterjee
*
* @param ...
*/
function pra()
{
$args = func_get_args();
if (_is_array($args)) {
$counter = 1;
foreach ($args as $val) {
pr($val, $counter);
$counter++;
}
}
}
/**
* Prints all specified arguments with their variable name and exits
*
* @author Soubhik Chatterjee
*
* @param ...
*/
function prad()
{
$args = func_get_args();
if (_is_array($args)) {
$counter = 1;
foreach ($args as $val) {
pr($val, $counter);
$counter++;
}
}
exit;
}
/**
* var dumps and dies
*
* @author Soubhik Chatterjee
* @param string $text
* @param string $label
*/
function vd($text, $label = ''){
echo '<pre>',"\n";
echo '==', $label, '==', "\n";
var_dump($text);
echo "\n",'</pre>', "\n\n";
}
/**
* var dumps and dies
*
* @author Soubhik Chatterjee
* @param string $text
* @param string $label
*/
function vdd($text, $label = ''){
echo '<pre>',"\n";
echo '==', $label, '==', "\n";
var_dump($text);
echo "\n",'</pre>', "\n\n";
exit;
}
/**
* Logs any data
*
* @author Soubhik Chatterjee
* @param mixed $data
* @param string $label
*/
function logg($data, $label = '') {
$file = '/Users/soubhik/Documents/tmp/my-errors.log';
error_log('========' . $label . '========' . "\n", 3, $file);
error_log(json_encode($data) . "\n", 3, $file);
error_log( '========' . $label . '========'. "\n\n", 3, $file);
}
/**
* @author Soubhik Chatterjee <soubhik.chatterjee@kayako.com>
* @param mixed $data
* @return string
*/
function pt($data)
{
if (is_object($data)) {
echo get_class($data);
} else if (is_bool($data)) {
var_dump($data);
} else if (is_array($data)) {
pr($data);
} else {
echo $data . ' [' . gettype($data) . ']';
}
}
/**
* @author Soubhik Chatterjee <soubhik.chatterjee@kayako.com>
* @param mixed $data
* @return string
*/
function ptd($data)
{
if (is_object($data)) {
echo get_class($data);
} else if (is_bool($data)) {
var_dump($data);
} else if (is_array($data)) {
pr($data);
} else {
echo $data . ' [' . gettype($data) . ']';
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment