4

I am trying to declare a constant for our office names of each country with associative array.

My declaring code is as below:

define( "OUR_OFFICE", [
    "Japan" => "Tokyo Shibuya Office",
    "Taiwan" => "Taipei Shilin Office",
    "Korea" => "Seoul Yongsan Office",
    "Singapore" => "Singapore Novena Office",
    "Australia" => "Sydney Darlinghurst Office"
]);

However, it just shows message:

Warning: Constants may only evaluate to scalar values

Is it possible to declare a constant with associative array?

Thank you very much!!!

Ndroid21
  • 400
  • 1
  • 8
  • 19
Ed Chen
  • 125
  • 1
  • 1
  • 13
  • 1
    `Constants may only evaluate to scalar values` isn't that self descriptive? Why you need that in the first place, you can have a class constant instead. – Mike Doe Jul 07 '17 at 07:26
  • Short answer: no, this is not possible. – arkascha Jul 07 '17 at 07:26
  • @arkascha Thanks for your responses. But is there any difference between my example and this example on the PHP.NET ?http://php.net/manual/en/function.define.php#120902 I'm just trying to do the same thing like this one. – Ed Chen Jul 07 '17 at 07:42
  • 2
    This is NOT a duplicate question, as this one asks about associative arrays – Stephen R Oct 24 '17 at 16:37

2 Answers2

11

The code you posted doesn't work on PHP 5.

Declaring constant arrays using define is a new feature introduced in PHP 7.0.

Since PHP 5.6 it is possible to define a constant array using the const keyword:

const OUR_OFFICE = [
    "Japan"     => "Tokyo Shibuya Office",
    "Taiwan"    => "Taipei Shilin Office",
    "Korea"     => "Seoul Yongsan Office",
    "Singapore" => "Singapore Novena Office",
    "Australia" => "Sydney Darlinghurst Office",
];

The documentation highlights the differences between define() and const:

As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/catch blocks.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • 1
    Can anyone actually point to documentation confirming that you can use **associative** arrays as in the example above? It doesn't work for me, and I can't find any other reference to that usage. – Stephen R Oct 24 '17 at 17:03
  • 1
    @StephenR the third sentence of the answer links to the page about the new features introduced in PHP 5.6, on the official PHP site. The word "documentation" in the sentence after the code block points to the documentation page about constants. It explains in the second paragraph that the feature is available since PHP 5.6. Please also check this test: https://3v4l.org/43Fd1 – axiac Oct 24 '17 at 18:08
  • For some reason it only works if I use define(), but otherwise it works. Thanks – Stephen R Oct 24 '17 at 20:42
  • My issue was that I kept seeing pages say you could assign arrays, but every example was an indexed array, not associative. Your response here is the ONLY example I've seen explicitly saying you can use associative. Seems to me an oversight in the PHP documentation -- your information is correct :-) – Stephen R Oct 24 '17 at 20:46
  • 1
    @StephenR all PHP arrays are associative arrays (aka maps, aka hashes, aka dictionaries). The numeric indexed arrays are a special case that happens when the keys are not provided (and PHP uses numeric increasing keys as default). – axiac Oct 25 '17 at 07:06
3

In PHP 7, array values are also accepted.

But prior PHP 7, you maybe can do this, to pass an array elsewhere using define:

$array = [
    "Japan" => "Tokyo Shibuya Office",
    "Taiwan" => "Taipei Shilin Office",
    "Korea" => "Seoul Yongsan Office",
    "Singapore" => "Singapore Novena Office",
    "Australia" => "Sydney Darlinghurst Office"
];

$define_array = serialize($array);

define( "OUROFFICE", $define_array );

$our_office = unserialize(OUROFFICE);

print_r($our_office);
Ndroid21
  • 400
  • 1
  • 8
  • 19
  • I see ~~~~~ I 'd better have my php updated...Thank you very much ! – Ed Chen Jul 07 '17 at 08:10
  • 1
    Am I missing something? I don't get why there are upvotes on this answer! How is this solution related to `PHP7`? Then, using `serialize/unserialize` is very inconvenient. Why not then just save the entire thing with an unusual variable name (that you know will never be overwritten) and include it from a file? BTW, this solution works with any version of PHP, but I won't recommended it. One can't remember which constants to `unserialize` and which not to (of course, you can save all constants as serialized strings but is it really worth? writing a class for that is better then) – Fr0zenFyr Mar 12 '19 at 07:29