Why PHP casts two numerical strings to numbers before [loosely] comparing them?

前端 未结 3 1748
孤城傲影
孤城傲影 2021-01-17 12:51

I browsed through several similar questions, but they all only state the fact:

If ... comparison involves numerical strings, then each string is conve

3条回答
  •  甜味超标
    2021-01-17 13:01

    Because, PHP produce a product for End-Users, not for Application Developers. So when you produce such product like below:

    if (isset($_POST['submit'])){
      if ($_POST['myinput'] == "1") echo 'Yes'; //or == 1
      else echo 'NO';
    }
    
        ?>
        

    If the user enter 1 or 0001, what do you expect to print in both case? Yes or NO?

    the answer is clear. we expect to see Yes. so this is why PHP does so If for any rare reason, we need to definitely compare them, then we should change == to ===

提交回复
热议问题