PHP int 类型转换生成错误的值 [英] PHP int typecasting generating wrong value
问题描述
我想在数据库中存储整数值,为此我在 $_POST 值上使用 (int) 类型转换.但它评估另一个值而不是提交的整数.
I want to store integer value in database, and for that im using (int) type casting on $_POST values. But its evaluating another value instead of submitted integers.
echo (int)0444444;
输出
149796
请帮忙.
推荐答案
以0
为前缀的整数意味着它应该被解释为一个八进制值,即基数8 而不是基数 10.
Prefixing an integer with 0
means it should be interpreted as an octal value, that is, base 8 instead of base 10.
0444444 (OCT) = 149796 (DEC)
如果要将字符串转换为整数,而不是强制转换,可以使用 intval()
可以选择允许您指定基数 10.
If you want to convert a string to an integer, instead of casting, you can use intval()
which optionally will allow you to specify base 10.
尽管您的示例中的转换是从 int 到 int 的转换(0444444 已被解释为整数),因此您的转换是空操作.
The cast in your example though is a cast from an int to an int (0444444 already being interpreted as an integer), so your cast is a no-op.
这篇关于PHP int 类型转换生成错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!