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

From: Al (php.newsgroups_at_allysays.com)
Date: 10/31/03


To: php-general@lists.php.net
Date: Fri, 31 Oct 2003 13:58:57 +1100


> 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;
    }
}



Relevant Pages

  • Re: Problem with "/n" in Firefox
    ... form and display the values on the browser. ... The code work fine except in Firefox/Opera, ... var thisDoc = document.forms ... If you want to go with using the tags, ...
    (comp.lang.javascript)
  • Re: Problem with "/n" in Firefox
    ... form and display the values on the browser. ... The code work fine except in Firefox/Opera, ... var thisDoc = document.forms ... displayOrderArray=new Array ...
    (comp.lang.javascript)
  • Re: Problem with "/n" in Firefox
    ... form and display the values on the browser. ... The code work fine except in Firefox/Opera, ... var thisDoc = document.forms ... displayOrderArray=new Array ...
    (comp.lang.javascript)
  • Re: Problem with "/n" in Firefox
    ... form and display the values on the browser. ... The code work fine except in Firefox/Opera, ... var thisDoc = document.forms ... displayOrderArray=new Array ...
    (comp.lang.javascript)
  • Re: Problem with "/n" in Firefox
    ... form and display the values on the browser. ... The code work fine except in Firefox/Opera, ... var thisDoc = document.forms ... displayOrderArray=new Array ...
    (comp.lang.javascript)