Ehsanul Haque

Welcome to my personal site & blog

Archive for October, 2007

25 October
4Comments
05 October
3Comments

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.