-1

This is as simple as it gets, but I can't see why I'm getting 0 when I echo $num[3] from this PHP array.

$num=[00000004,00000002,00000005,00000009]; echo $num[3];

CheeseFlavored
  • 1,922
  • 1
  • 21
  • 28

4 Answers4

2

Because of the leading zero's, it needs to be a string, not a number:

$num=['00000004','00000002','00000005','00000009']; echo $num[3];
Gert B.
  • 2,282
  • 18
  • 21
2

Based on Integers Manual

To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.

Your values are actually representing octal notation and that's why you are leading to issue.

Convert them to string:

$num=[
    '00000004',
    '00000002',
    '00000005',
    '00000009'
  ]; 
echo $num[3];

Output: https://3v4l.org/ktsJY

Note:- From Php7 onward it will give you Parse error: Invalid numeric literal

https://3v4l.org/W3aD0

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • @GertB AlivetoDie's answer mentioning "octal" was the real problem. So this is the best answer. It is acceptable to not use the quotes in php5, but the results were showing octal numbers. – CheeseFlavored Feb 11 '21 at 13:24
  • True, it is the most complete answer. Adding the php7 note was a very good idea btw. I just think that its a lot of information for a simple mistake. – Gert B. Feb 11 '21 at 13:30
1

The data is not stored correct, they are stored as a number but they are strings so they need to be stored as a string. When I run your code it throws an error

Parse error:  Invalid numeric literal in [...][...] on line 2

The code below wil return 00000009

$num=['00000004','00000002','00000005','00000009']; echo $num[3];
Jasper B
  • 851
  • 4
  • 13
-1

Your syntax is not correct,try this

$num = array("00000004","00000002","00000005","00000009");
echo $num['3'];
J.Ongoma
  • 65
  • 1
  • 5