Variable "probably undefined" - am I missing something?

1.6k views Asked by At

Sanity check. I am still pretty new/dumb when it comes to OOP PHP.

PhpStorm (PS) warns that "$nowGmt" is "probably undefined". Is PS being over fussy or am I missing something obvious (looks good to me).

public function nowUTC($type)
{
  if ($type=="mysql") $nowGmt=gmdate("Y-m-d H:i:s");
  elseif ($type=="text") $nowGmt=gmdate("M jS Y g:i a");
  elseif ($type=="unix") $nowGmt=time();
  return $nowGmt;
}

Thanks for any pointers.

PS I should say for those of you not familiar with PS it is generally fantastic but occasionally a bit odd in some of its warnings. (Well the warnings probably reflect good practice and I am probably at fault.) It is the first IDE that I think is ****. Just did not want to put you off trying PS.

1

There are 1 answers

0
Marc On

By not offering an else case where the value of $nowGmt is set to some value and also by not having $nowGmt defined before the if statments, the warning is correct.

Imagine that it does not enter in any if or ifelse case, then the variable $nowGmt would not exist yet the function wants to return it.

You should include at least one initialization value to $nowGmt.