0

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

Melvin Rey
  • 147
  • 3
  • 17

2 Answers2

1

If you're running PHP7 you should probably take a look at this answer.

As said in the linked answer:

This comes from the changes made to how integers, specifically octals, are handled in PHP7 (as oppsoed to PHP5).

Invalid octal literals

Previously, octal literals that contained invalid numbers were silently truncated (0128 was taken as 012). Now, an invalid octal literal will cause a parse error.

Either use them as strings, or actual integers

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

$a = array("00001", "00008", "00009", "00012"); // Strings

You can find more info on Integers/Octals here: http://php.net/manual/en/language.types.integer.php

Brian Moreno
  • 977
  • 4
  • 11
  • 39
  • I'm not sure If I'm using PHP 5.6 or PHP 7 version right now. I'll check my PHP later, Right now the problem is that it only display "0" – Melvin Rey Jun 08 '17 at 05:36
0

With full credit to Qirel and his answer here - https://stackoverflow.com/a/40736053/2288334

This is likely due to how PHP is handling what used to be silently parsed to octal literals now is not. Basically, PHP7 thinks you're trying to pass an octal as the only value of the array, but it is invalid.

For Reference: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.integers.invalid-octals http://php.net/manual/en/language.types.integer.php

pxslip
  • 299
  • 1
  • 6
  • If it is a duplicate on SO, do not repost a solution. Please remove your answer and flag to close as duplicate. – mickmackusa Jun 08 '17 at 03:45