Last active
November 28, 2016 09:55
-
-
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;
This file contains 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 | |
/** | |
* Print with formatting and Die. Used for debugging purpose. | |
* | |
* @author Soubhik Chatterjee <[email protected]> | |
* @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 <[email protected]> | |
* @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 <[email protected]> | |
* | |
* @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 <[email protected]> | |
* | |
* @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 <[email protected]> | |
* @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 <[email protected]> | |
* @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 <[email protected]> | |
* @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 <[email protected]> | |
* @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 <[email protected]> | |
* @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