0

I have a function that takes an input, where $input comes from a MySQL database and is either an integer, a string, or null value, and returns one of three strings based on the following conditions:

  • If it is an int that's equal to 0, return something

  • Elseif it is an empty string, return something else

  • Else return something else

My code looks like this:

if ($input == "") {
  return "case1";
}
elseif ($input == "0") {
  return "case2";
}
else {
  return "case3";
}

After echoing each input and comparing the output, it doesn't seem to work. Any ideas? I've tried using every combination of "0" and 0, == and ===.

In testing, whole numbers and strings work, an so do empty strings. But an input of integer 0 does returns case3.

  • What is the input that you supplied in your tests, and which ones did not provide the output you expected? – Jeff Lambert May 07 '18 at 20:08
  • *"Any ideas?"* -- when everything else fails, read the documentation: http://php.net/manual/en/types.comparisons.php – axiac May 07 '18 at 20:09
  • 4
    PHP uses dynamic typing, so "", 0, and FALSE are all equivalent. You need to use === in your comparison. http://php.net/manual/en/language.operators.comparison.php This page explains how the types convert: http://php.net/manual/en/types.comparisons.php – csyria May 07 '18 at 20:09
  • Have you tried with `$input === 0`? This check type and value – Chopi May 07 '18 at 20:10
  • 1
    I know there is a dupe for this. Just gotta find it – John Conde May 07 '18 at 20:10
  • http://php.net/manual/en/language.types.boolean.php – Andrew May 07 '18 at 20:12

0 Answers0