0

We scratch our heads about how this result is achieved:

<?php
  $str = 'bg013.jpg';

  $a = $str != 0;
  $b = $str == 0;
  $c = $str === 0;

  var_dump($a);
  var_dump($b);
  var_dump($c);
?>

Results in:

bool(false)
bool(true)
bool(false)

Is there anything I missed? Suspicious, isn't it?

izk
  • 234
  • 6
  • 19
Jonathan
  • 1,955
  • 5
  • 30
  • 50
  • 6
    If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value. --> http://php.net/manual/en/language.operators.comparison.php – pes502 Mar 17 '16 at 08:35
  • imho, @pes502 might as well have posted that as an answer rather than a comment. this kind of behavior is inherent on weakly typed languages / constructions. – Timothy Groote Mar 17 '16 at 08:39
  • 5
    @TimothyGroote I don't want to post my comment as answer, because it's official text from PHP doc – pes502 Mar 17 '16 at 08:42
  • @pes502 Haha, sorry. it just made a lot of sense when i read it. didn't check the PHP doc. – Timothy Groote Mar 17 '16 at 08:45
  • Not suspicious in the least, simply documented behaviour of loose typed comparisons – Mark Baker Mar 17 '16 at 09:31
  • If I change the string to "3.jpg" it changes the result. That is - to me - not straight forward. – Jonathan Mar 17 '16 at 12:09

0 Answers0