"Array item" and constant "define" shows only 0 and not the whole number

时间:2017-06-08 03:02:03

标签: php arrays

I declared an array item "0859" but when I display the array item, it shows only the 0 and not the whole number "0859".

In constant define I set the value to "01238" the result is the same with my array, it shows only the value 0 and not the whole number 01238. I would like to know further about the reason why array item and define that starts with 0 in there item and values shows only the 0 and not the whole number

Array

<?php
  $num = [0859];
  echo $num;

  output: 0

  Why not the the output is "0859"
?>

Constant Define

<?php

  define(myNum, 01238);
  echo myNum;

  output: 0

  Why not the the output is "01238"

?>

I'm using Wamp PHP version 5.5.12

Please enlighten me why I get the output only 0 and not the whole number

2 个答案:

答案 0 :(得分:1)

如果你正在运行PHP7,你应该看一下this的答案。

如链接答案所述:

  

这来自对整数,特别是八进制,   在PHP7中处理(与PHP5相反)。

     

八进制文字无效

     

以前,包含无效数字的八进制文字是   默默地截断(0128被视为012)。现在,一个无效的八进制   literal将导致解析错误。

     

将它们用作字符串或实际整数

     

$ a = array(1,8,9,12); //整数

     

$ a = array(“00001”,“00008”,   “00009”,“00012”); //字符串

您可以在此处找到有关整数/八进制的更多信息:http://php.net/manual/en/language.types.integer.php

答案 1 :(得分:0)

完全归功于Qirel及其答案 - https://stackoverflow.com/a/40736053/2288334

这可能是由于PHP如何处理以前默认解析为八进制文字的内容。基本上,PHP7认为你试图将八进制作为数组的唯一值传递,但它是无效的。

供参考: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.integers.invalid-octals http://php.net/manual/en/language.types.integer.php