8

I'm looking for an alternative for define('name', array) as using an array in define gives me this error:

Constants may only evaluate to scalar values in ...

The array I'm mentioning contains strings only.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anoniem Anoniem
  • 1,915
  • 6
  • 18
  • 19
  • 1
    `array` is what? has to be `The value of the constant; only scalar and null values are allowed. Scalar values are integer, float, string or boolean values.` – Hanky Panky Oct 18 '13 at 07:46
  • This approach deserves a use-case. Why would you want to do this? defines are global symbols usually used for things like config. There are better approaches, such as a dependency injection. – webbiedave Oct 18 '13 at 07:53

5 Answers5

22

From php.net...

The value of the constant; only scalar and null values are allowed. Scalar values are integer, float, string or boolean values. It is possible to define resource constants, however it is not recommended and may cause unpredictable behavior.

But You can do with some tricks :

define('names', serialize(array('John', 'James' ...)));

& You have to use unserialize() the constant value (names) when used. This isn't really that useful & so just define multiple constants instead:

define('NAME1', 'John');
define('NAME2', 'James');
..

And print like this:

echo constant('NAME'.$digit);
Jenson M John
  • 5,499
  • 5
  • 30
  • 46
7

This has changed in newer versions of PHP, as stated in the PHP manual

From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant.

hutch
  • 163
  • 3
  • 7
2

If you are on php5.6 and you know that this version onward php does support arrays as constants. But you are still getting the following warning...

Warning: Constants may only evaluate to scalar values

Then you are in luck. Its because the method of defining array constants using define() is still not introduced in this version and has only been introduced inside phpv7.xx

So instead you can use the const keyword.

const MY_SUPER_CONSTANT = array(
    'cool_key'      => 'Cool Value',
    'ultra_fab_key' => 'Fabulous Value',
);
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
0

Define only works to define constants; The manual states:

The value of the constant; only scalar and null values are allowed. Scalar values are integer, float, string or boolean values. It is possible to define resource constants, however it is not recommended and may cause unpredictable behavior.

which is why an array won't work.

So for an array of constants, you could consider building a class that contains these constants, and can return them based on an array-type call?

Jelle Ferwerda
  • 1,254
  • 1
  • 7
  • 13
0

You can use json_encode function to encode the array as json string and define it as a constant to be used in any place where the constant is accessible.

Example :

// Define the array

$MyArray = array(
    "mykey"  => "myvalue",
    "mykey1" => "myvalue1"
);
define('MYARRAY', json_encode($MyArray));

// Decode the constant and use.
$UseMyArray = json_decode(MYARRAY);
patricus
  • 59,488
  • 15
  • 143
  • 145