Last active
August 29, 2015 14:20
-
-
Save PieterScheffers/da43ebc872559f200786 to your computer and use it in GitHub Desktop.
PHP - Functions
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
// Debug function | |
// print_r with pre tags | |
// optionally exit program | |
function pr($var, $exit=false) { | |
echo '<pre>'; | |
print_r($var); | |
echo '</pre>'; | |
if( $exit ) exit(); | |
} | |
// access a propery of an object or an array | |
// return a default value when not present / empty | |
function def($var, $prop, $default='') { | |
if( !empty($var) ) { | |
if( is_array($var) && !empty($var[$prop]) ) { | |
return $var[$prop]; | |
} else if( is_object($var) && !empty($var->$prop) ) { | |
return $var->$prop; | |
} | |
} | |
return $default; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment