Skip to content

Instantly share code, notes, and snippets.

@soubhikchatterjee
Last active November 28, 2016 09:55
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/8a52f353e65e655772cfd9a11f41abbb to your computer and use it in GitHub Desktop.
Save soubhikchatterjee/8a52f353e65e655772cfd9a11f41abbb to your computer and use it in GitHub Desktop.
Debugging helper functions for your php code. Easy to write prd() instead of echo '<pre>'; print_r(); echo '</pre>'; die;
<?php
/**
* Print with formatting and Die. Used for debugging purpose.
*
* @author Soubhik Chatterjee <soubhik@chatterjee.pw>
* @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 <soubhik@chatterjee.pw>
* @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 <soubhik@chatterjee.pw>
*
* @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 <soubhik@chatterjee.pw>
*
* @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 <soubhik@chatterjee.pw>
* @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 <soubhik@chatterjee.pw>
* @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 <soubhik@chatterjee.pw>
* @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.pw>
* @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.pw>
* @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