【发布时间】:2012-10-15 05:52:23
【问题描述】:
我在阅读 PHP 手册时遇到了type juggling
我很困惑,因为我从来没有遇到过这样的事情。
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
当我使用此代码时,它返回 15,加起来是 10 + 5,当我使用 is_int() 时,它返回 true,即。 1 我期待一个错误,它后来引用我到 String conversion to numbers 我读到 If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero)
$foo = 1 + "bob3"; /* $foo is int though this doesn't add up 3+1
but as stated this adds 1+0 */
现在,如果我想将 10 Little Piggies OR bob3 视为 string 而不是 int,我该怎么办。使用settype() 也不起作用。我想要一个无法将 5 添加到字符串的错误。
【问题讨论】:
-
-1 这个问题,认真的吗? com'on coward 过来回答我,为什么无缘无故地连续否决我的问题和答案?
标签: php type-conversion