php - Why is 02000 evaluated to 1024
问题描述
Out of curiosity, I wrote a program like this:
auto number1 = 100;
auto number2 = 02000;
auto number3 = 2;
auto result = (number1 + number2) / number3;
std::cout << result;
Interestingly, the program outputs 562. So, in visual studio, i hovered over the variable "number2" and it showed (int) 1024
. I didn't understand why this happened. So, I tried its equivalent in php which was this:
$number1 = 100;
$number2 = 02000;
$number3 = 2;
$result = ($number1 + $number2) / $number3;
echo $result;
The result was the same 562. What is it that I am missing because if I remove the zero in front of number2 to make it '2000', it shows 1050 as expected
解决方案
It treats 02000 as octal number and converts to decimal. Anythings that starts with preceding 0 is considered octal number.
0 2 0 0 0 = 0*8*8*8*8 + 2*8*8*8 + 0*8*8 + 0*8 + 0
= 0 + 2*512 + 0 + 0 + 0
= 1024
Now 1024 is number2.
result = (100 + 1024)/2
= 1124/2
= 562
推荐阅读
- r - 因子变量中的缺失值问题
- google-sheets - 你如何获得一个谷歌表格列来更新现有谷歌课堂帖子的成绩?
- r - 错误:在“df”中找不到名为的列
- reactjs - gh-pages 只发布背景图片
- primefaces - IBM AppScan - 盲 SQL 注入(基于时间) - JSF 2.2 和 Primefaces - JBOSS 7.2 EAP
- typescript - 如何在打字稿中定义“set-cookie”为“string []”但任何其他键为“string”的类型?
- r - 用 facet_zoom 放大分类图?
- python - 如何使标签在 python 中的 seaborn 中看起来正常?
- angular - Chrome 是否有类似 Postman 的 SSL 证书验证设置?
- python - 如何检查 Python dict 是否有特定的键