0

I have a simple associative array declared like the following:

static private $foo = [
        16 => 'xyz',
        7 => 'x',
        8 => 'y',
        9 => 'xy'
];

When I run a syntax check on this declaration, I get the following:

Parse error: syntax error, unexpected '16' (T_LNUMBER), expecting ']

What am I doing wrong here?

6
  • 2
    You're missing the last single quote after the 9 => 'xy' and the static and private keywords are the wrong way around. Commented Jul 13, 2015 at 19:54
  • sorry that was just a typo when I pasted into here. the single quote is actually there in the real code. Commented Jul 13, 2015 at 19:55
  • 2
    No worries. Swap static and private so you have private static $foo Commented Jul 13, 2015 at 19:55
  • Tried that as well @BenSwinburne unfortunately no cigar Commented Jul 13, 2015 at 19:56
  • 1
    private static- static private make no difference to the parser ;-) Since this is obviously part of a class definition can you please post a complete script? Try to create the smallest yet complete script possible that exhibits the behaviour you've described. Commented Jul 13, 2015 at 19:58

5 Answers 5

6

There are some weird characters (probably BOMs, I wasn't checking it) at the start of each line before an array element (see http://3v4l.org/ZnqIp). These characters are invisible, so their presence can cause strange errors like this one.

They show when pasted into Notepad++ as small squares:

Image showing characters in Notepad++

I removed them, try this code:

<?php

class Foo {
    static private $foo = [
        16 => 'xyz',
        7 => 'x',
        8 => 'y',
        9 => 'xy'
    ];
}

It should work just fine (see also http://3v4l.org/WQGWA).

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

1 Comment

Nice. I get ASCII values 226, 128, 130, and 32 for various parts of the whitespace
3

If you take a look at the copy/pasted code in any hex editor you'll notice strange invisible characters which are confusing PHP. Just do a clean copy/paste and you are good to go.

And while you are at it, check your editor configuration/encoding.

EDIT: A simple Unicode lookup says its the so called EN SPACE character (hex sequence E2 80 82). How you managed to put it in your code is beyond me.

enter image description here

1 Comment

It is probably cut-n-pasted from a webpage using css content
1

I just tried out with copy&paste your code. For some reason I got the same error. Then deleted all whitespaces in array and again added spaces. Now everything worked out. Have no clue why thats happening. Just try to define array like

 private static $foo = [16 => 'xyz', 7 => 'x', 8 => 'y', 9 => 'xy'];

And if that works out - add line breaks. Some copy & paste magic is messing up things.

Comments

0

I think I figured out what is going on with your code. It has nothing to do with static private.

I copied your code above and ran a test and was getting the same parse error. I restructured your array and it seems you added spaces and not tabs. I'm relatively new to PHP but, I replaced spaces with tabs and it seems to work.

private static $foo = [
    16 => 'xyz',
    7 => 'x',
    8 => 'y',
    9 => 'xy'
];

1 Comment

PHP is whitespace-insensitive, except in a few key areas.
0

Try this

private static $foo = array( 
  16 => 'xyz', 
  7 => 'x', 
  8 => 'y', 
  9 => 'xy'
);

Comments

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.