PHP Function I frquently use
While developing the web applications with PHP I have made my own small function library. In this library I have few functions that I use very frequently. One of the functions that is used more often is echo_d(). The purpose of this function is to check a variable to see if it is empty or not set; then based on the check result it either prints a default value or the value of the variable.
This is a simple function that saves me a lot of time and keeps my main codes cleaner. So here it is.
function echo_d($var, $def) {
$var = trim($var);
echo ((isset($var)) && (!empty($var))) ? $var : $def;
}
When to use this function? One of the most common places I use this function is in the reports. When I am generating reports for anything I always have to check whether certain value is in the database; it prints a default value if it cannot find one. The usage of this function in the scenario is something like:
$lastLoginDate = $rowsFromDB['last_login_date']; echo_d($lastLoginDate, "---");
This function can be expanded to make it more versatile but I have found this tiny little function very useful.
–
Thank you.
![[del.icio.us]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/facebook.png)
![[Feed Me Links]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/feedmelinks.png)
![[Google]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/linkedin.png)
![[StumbleUpon]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://ehsan.bdwebwork.com/wp-content/plugins/bookmarkify/twitter.png)


