1

I'm having a strange problem in some of my php. What seems to be happening is that 0 is being shown as equal to the string "done". Here's what's happening:

if(!isset($pointer)){
    $pointer = 0;   
}
error_log($pointer); //in this instance, I haven't set a pointer, returns 0
if($pointer == "done"){
    die();
}

For some reason, the second if statement is triggering and killing the script. I can't figure out why, when $pointer is equal to 0, it is apparently also equal to "done". Is this something super easy that I'm just overlooking?

I've worked around the situation, using === on the second if statement gives me the desired result, I would just like to understand why it wasn't working in the first place. Thank you for your time.

Eric Strom
  • 715
  • 9
  • 20
  • Uses the `===` to check for equality AND same data type. – sachleen Sep 19 '12 at 18:46
  • http://php.net/manual/en/language.operators.comparison.php http://php.net/manual/en/language.types.type-juggling.php http://php.net/manual/en/types.comparisons.php http://php.net/manual/en/language.types.php that was hard – Dejan Marjanović Sep 19 '12 at 18:46

1 Answers1

6

It's a type comparison issue; as you say, using:

if($pointer === "done"){

forces a check against both value and type.

What is happening is that 'done' is being converted into a number for the comparison. Since there are no numbers in there, it's coming out as null. And null evaluates to 0 in your comparison.

andrewsi
  • 10,807
  • 132
  • 35
  • 51