This question already has an answer here:
- PHP Constants Containing Arrays? 19 answers
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!!!
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",
];
As opposed to defining constants usingdefine()
, constants defined using theconst
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 ortry
/catch
blocks.
0 comments:
Post a Comment