DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

An Analogue Of ColdFusion's CFPARAM Tag In PHP.

07.22.2005
| 4823 views |
  • submit to reddit
        
/**
 * Checks if $var is already defined and if not, sets it to
 * $default.
 *
 * @param  $var         Variable to check.
 * @param  $default     Value to give the checked variable if
 *                      found to be null; "" by default.
 * @param  $catchNulls  TRUE: treat empty strings as null; FALSE
 *                      by default.
 *
 * @note   It's a lot like ColdFusion's <CFPARAM> tag.
 */
function set_default(&$var, $default="", $catchNulls=FALSE) {
    if (!isset($var) || ($catchNulls && $var == "")) {
        $var = $default;
    }
}
This appears to have stopped working as of PHP 5.