0
static $searchQuery="nothing";

if ($searchQuery=="nothing")
{
    if ($referral=="")
    {
        $referral=referrer();
    }
    $searchQuery=getSearchQuery($referral);
}
return $searchQuery;

Basically I need to initialize $searchQuery with something.

I can initialize it with "" but the value of $searchQuery is often legitimately "".

So how should I initialize it? ""? Nil? Null? array()? What?

I can also use

static $result;
if  (isset($result))
{
    return $result;
}

but I got warning because $result is not defined.

4
  • You could ditch the static for starters. Commented Jan 18, 2013 at 17:03
  • what? Of course I have to use static. Commented Dec 13, 2013 at 0:18
  • Why's that? Why do you think you need static? Commented Dec 13, 2013 at 8:42
  • Please start by explaining what you are trying to do. (Leave implementation details aside) Commented Dec 13, 2013 at 9:00

1 Answer 1

2

It's up to you, but you need to be careful with PHP's loose typing. I prefer NULL, and I would write that code as:

static $searchQuery = NULL;

if( is_null($searchQuery) ) {
    if( emtpy($referral) ) {
        $referral=referrer();
    }
    $searchQuery=getSearchQuery($referral);
}
return $searchQuery;
Sign up to request clarification or add additional context in comments.

2 Comments

Is NULL equal to 0? 0 is a possible value to return by the way.
@JimThio In PHP's usual 'lazy' comparison NULL, 0, "0", and "" are all equivalent to false. is_null() is a strict comparison equivalent to $value === NULL and will only return true if the value is NULL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.