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

6
  • Why not put it as a string already ? Like this $num = ["0859"]; Commented Jun 8, 2017 at 3:06
  • What version of PHP are you running? Commented Jun 8, 2017 at 3:09
  • 1
    Check this link php.net/manual/en/language.types.integer.php Commented Jun 8, 2017 at 3:19
  • 2
    Possible duplicate of Parse error: Invalid numeric literal Commented Jun 8, 2017 at 3:22
  • @GeekSilva no I don't want to put the number as a string. Commented Jun 8, 2017 at 5:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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"
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

1 Comment

If it is a duplicate on SO, do not repost a solution. Please remove your answer and flag to close as duplicate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.