inicio mail me! View Ehsanul Haque's LinkedIn profile sindicaci;ón

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.

rick said,

October 7, 2007 @ 4:07 am

good one. will use it. thx

Tauhid said,

October 9, 2007 @ 6:12 am

Good function Eshan vai. Emon aro thakle please share korben.

RSS feed for comments on this post · TrackBack URI

Leave a Comment