Understanding PHP Type Coercion

前端 未结 4 664
梦谈多话
梦谈多话 2020-12-03 15:34

I saw this small piece of code that is evading my understanding:



        
4条回答
  •  清歌不尽
    2020-12-03 16:08

    PHP attempts to convert to type float because the string begins with a 0. It stops after 0 because the next character is not a number. The same thing happens when you use type coercion to convert scientific notation to integer:

    $x = (float)"12E-1x";  // $x == 1.2
    $x = (int)"12E-1x";  // $x == 12 (stops at E because it's not an integer)
    

提交回复
热议问题