Chris O'Kelly
Chris O'Kelly

Reputation: 1893

Unexpected Double Quote in Array definition

I am seeing a parse error in the definition of an array as a private, static member of a class.

The code in question is:

// %1$s for table name
// %2$s for character set collate
// Version 1 is the initial DB layout
private static $version_sql = array(
    1   =>  "CREATE TABLE %1$s (
                id int(11) NOT NULL AUTO_INCREMENT,
                chance_id int(11) UNSIGNED,
                user_fingerprint varchar(32),
                state tinyint UNSIGNED NOT NULL DEFAULT 0,
                expires int(11) UNSIGNED,
                email varchar(255),
                PRIMARY KEY  id (id)
                UNIQUE KEY chance_user (chance_id,user_fingerprint)
            ) %2$s;"
);

The parse error raised is Parse error: syntax error, unexpected '"', and the line number given is that of the first line of the SQL definition (the one with CREATE TABLE on it).

I've read Parse Error: Unexpected double quotes in array which is a similar error but seems to be more to do with the use of string interpolation in a context where the referenced variable is not available, which is not something I am doing here.

Any idea what I'm missing here?

Upvotes: 1

Views: 831

Answers (1)

Kep
Kep

Reputation: 5857

You need to escape your $ chars since your string contains $s in double-quotes. PHP tries to fill in the contents of that variable (which is not allowed in static context).

Either replace your $ with \$ or use single-quotes for the whole string.

// %1$s for table name
// %2$s for character set collate
// Version 1 is the initial DB layout
private static $version_sql = array(
    1   =>  'CREATE TABLE %1$s (
                id int(11) NOT NULL AUTO_INCREMENT,
                chance_id int(11) UNSIGNED,
                user_fingerprint varchar(32),
                state tinyint UNSIGNED NOT NULL DEFAULT 0,
                expires int(11) UNSIGNED,
                email varchar(255),
                PRIMARY KEY  id (id)
                UNIQUE KEY chance_user (chance_id,user_fingerprint)
            ) %2$s;'
);

Upvotes: 2

Related Questions