1

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.

BeNice
  • 2,165
  • 2
  • 23
  • 38
  • 5
    If none of the 3 conditions are met (for whatever reason, `$type` could potentially have some other value or an empty string) what will be the value of `$nowGmt` at the return? – Paul T. Sep 05 '20 at 18:00
  • I see what you mean but "probably" to me means that more than 50% of time it will not work. I am only going to call it I want the `mysql` or `text` versions (don't think I use the `unix` version). If that is what it means that is a bit of an OTT warning! Thanks – BeNice Sep 05 '20 at 18:03
  • 1
    you need to make a choice of what you want for `else $nowGmt=...;` then add it, it's seeing that there is no else. also use `===` – Lawrence Cherone Sep 05 '20 at 18:05
  • 2
    Or define `$nowGmt = time();` before the IF block as the default value to output if `$type` doesnt match. – IncredibleHat Sep 05 '20 at 18:05
  • 3
    Currently, PhpStorm's code inspector has two types of ["undefined" warnings](https://www.jetbrains.com/help/phpstorm/php-undefined-variable.html): "this variable is definitely undefined" when it can't find a definition anywhere, or "this variable is probably undefined" when there's one or more ways to get to a point where that happens (like if `$type` is something other than `"mysql"`, `"text"` or `"unix"`). I suppose the message could be friendlier, e.g. "possibly undefined" instead of "probably undefined". – rickdenhaan Sep 05 '20 at 18:17
  • @LawrenceCherone I have never really understood the difference between `==` and `===`. Well I sort of do (that they also match in type) but I find the reality super weird and tend to go with the empirical - if it works it works. (I am only programming for myself here.) I would probably just hope this crashes horribly if I do not pass either `mysql` or `text`. Thanks for the pointers. – BeNice Sep 05 '20 at 18:25
  • @rickdenhaan u star. If this had said "possibly" I would not have wasted 30 mins here. Will feed that back to PhpStorm and stand down. (I thought I had somehow misunderstood public functions or something!). @LazyOne I never seem to use `switch` statements. Given how often I do these simple "chained" `ifelse`s I probably should. Thanks – BeNice Sep 05 '20 at 18:28
  • 2
    If you want to enforce proper usage of your function, you could add an assertion at the top of the function. Something like `if (!in_array($type, ["mysql", "text", "unix"])) throw new UnexpectedValueException("Unsupported type: " . $type);` – rickdenhaan Sep 05 '20 at 18:31
  • @rickdenhaan I believe you are speaking PHP 7. I only speak PHP 5 ... ok I am s l o w l y migrating. I studied computer science in the 70s - FORTRAN and Algol 68 were so much easier! – BeNice Sep 05 '20 at 23:20
  • 2
    @BeNice Well, PHP 5 was end-of-life'd on [31 December 2018](https://www.php.net/eol.php) :-) But no, this should work in PHP 5 as well. Short array syntax was introduced [in PHP 5.4](https://www.php.net/manual/en/migration54.new-features.php). – rickdenhaan Sep 07 '20 at 13:52

1 Answers1

2

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Marc
  • 46
  • 3