int variable with leading zero?

南笙酒味 提交于 2019-11-27 02:14:36

but a leading 0 does indicate octal in many languages, as is the case here.

It does have to do with octal numbers, 042 is interpreted as the octal number 42 which is 4 * 8 + 2 = 34.

Please be aware that the octal interpretation happens when the number literal is parsed while loading the PHP script. It has nothing to do with intval(), which doesn't do anything here because the value is already integer.

Octal interpretation happens only with number literals, not when casting a string to integer:

intval(042)   // == 34
intval('042') // == 42
(int)'042'    // == 42

In PHP a number with leading 0 is read as an octal number.

So 042 is read as an octal number.

The intval() function converts it into decimal number, which is 34.

So the browser output is 34.

It's simply how the function is defined. The leading zero is an instruction parse it as an octal number, similarly as to how 0x as a prefix means hex. See the documentation for more information.

Be careful when passing this function a string value with a leading "0". If you give it "042" then, it will treat it as BASE 8 - 9 and convert it to decimal value, which is by default base.

Please go through this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!