Re: Oh, for a "sureset()" (orthogonal to isset())

From: Date: Fri, 31 Oct 2003 02:58:57 +0000
Subject: Re: Oh, for a "sureset()" (orthogonal to isset())
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-168206@lists.php.net to get a copy of this message
> function sureset($var) { > > if(!isset($var) || empty($var)) > return ''; > else > return $var; > } > > Of course, when you've got strict checking on, the above doesn't > work, because if the variable is unset you get caught on the fact > before the function call happens. One change will make your code work: just pass the $var argument by reference, ie. function sureset (&$var) { ...code... } I wrote a similar function a while ago, with a little added functionality: function get_if_set ( &$testVar, $falseValue = NULL) { if ( isset( $testVar ) ) { return $testVar; } else { return $falseValue; } }

« previous php.general (#168206) next »