Type casting and Comparison with Loose Operator “==”

前端 未结 7 2288
暖寄归人
暖寄归人 2020-12-10 16:58

I have a problem baffling me terribly. I noticed this before but didn\'t give it any heed until today. I was trying to write my own check for integer strings. I know of

7条回答
  •  既然无缘
    2020-12-10 17:17

    Here's a function that more rigorously tests for either an int or an integer string.

    function isIntegerNumeric($val) {
      return (
        is_int($val)
          || (
            !is_float($val)
              && is_numeric($val)
              && strpos($val, ".") === false
          )
      );
    }
    

    It's relatively quick and avoids doing any string checking if it doesn't have to.

提交回复
热议问题