Alexey Tseitlin
Alexey Tseitlin

Reputation: 1309

Constant arrays define in php 7.0.0 converts to string

Why cant I do constant arrays in PHP 7? When I executing this code:

define(‘WEEKDAYS’, [ ‘Mon’, ‘Tue’ , ‘Wed’ , ‘Thu’ , ‘Fri’ ] );
var_dump(WEEKDAYS);
echo PHP_VERSION;

I get:

string(8) "WEEKDAYS"

7.0.0

Upvotes: 1

Views: 2220

Answers (1)

capcj
capcj

Reputation: 1535

You're not using the right syntax:

//Simple array

define("CONSTANT_ARRAY", ['one', 'two', 'three',]);

//Multidimensional array

define("CONSTANT_ARRAY_MULTIDIMENSIONAL", [
'fruits' => ['pear', 'apple', 'pineapple',],
'cars' => ['mustang', 'chevette', 'ferrari',],
'games' => ['streetfighter', 'lol', 'dota',],
]);

//Inside class you must use const keyword

const MYCONSTANT = ['pear', 'apple', 'pineapple',];

Upvotes: 5

Related Questions