是否可以将数组声明为常量[duplicate]

时间:2022-12-06 07:40:01

This question already has an answer here:

这个问题已经有了答案:

We can define a constant like

我们可以定义一个常数

define("aconstant','avalue');

Can't we define array in this fashion like below ?

难道我们不能像下面这样定义数组吗?

define("months",array("January", "February", ---); 

7 个解决方案

#1


58  

UPDATE: this is possible in PHP 7 (reference)

更新:这在PHP 7中是可能的(引用)

// Works as of PHP 7
define('ANIMALS', array(
    'dog',
    'cat',
    'bird'
));
echo ANIMALS[1]; // outputs "cat"

ORIGINAL ANSWER

原来的答案

From php.net...

从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.

常数的值;只允许标量值和空值。标量值是整数、浮点数、字符串或布尔值。可以定义资源常量,但是不建议这样做,并且可能导致不可预测的行为。

$months = array("January,"February",...) will be just fine.

$months = array(“一月”、“二月”、“…”)就可以了。

#2


77  

you can use const for that purpose since PHP 5.6 (via nikic).

您可以为此目的使用const,因为PHP 5.6(通过nikic)。

const months = ["January", "February"];
var_dump("January" === months[0]);

#3


29  

You can put arrays inside constants with a hack:

你可以把数组放在常量里面

define('MONTHS', serialize(array('January', 'February' ...)));

But then you have to unserialize() that constant value when needed and I guess this isn't really that useful.

但是当需要时,你必须不序列化()那个常量值,我想这并不是很有用。

As an alternative, define multiple constants:

另外,定义多个常量:

define('MONTH_1', 'January');
define('MONTH_2', 'February');
...

And use constant() function to look up the value:

使用constant()函数查找值:

echo constant('MONTH_'.$month);

#4


11  

No, you can't. See PHP: Syntax - Manual

不,你不能。参见PHP:语法-手册

Only scalar data (boolean, integer, float and string) can be contained in constants. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.

只有标量数据(布尔数据、整数数据、浮点数据和字符串)可以包含在常量中。可以将常量定义为资源,但应该避免它,因为它可能导致意外结果。

#5


9  

You can use JSON format to keep array in string and then assign this string to constant.

可以使用JSON格式将数组保存在字符串中,然后将该字符串赋值为常量。

$months = array("January","February","March");
define('MONTHS', json_encode($months));

When you want to use it:

当你想使用它:

$months = json_decode(MONTHS);

#6


6  

If you must have a constant, how about using a a delimited string and exploding into an array?

如果您必须有一个常量,那么使用一个带分隔符的字符串并爆炸成一个数组怎么样?

define("MONTHS", "January;February;March");
$months = explode(";",MONTHS);

#7


6  

As of PHP 5.6, it is possible to declare constant arrays. The linked documentation uses the example const ARR = ['a', 'b'];. You could also do const ARR = array('a', 'b');. However, in 5.6 there is an odd quirk: you can declare constant arrays using const, but not define(). This has been corrected in PHP 7.0.

在PHP 5.6中,可以声明常量数组。链接的文档使用示例const ARR = ['a', 'b'];还可以使用const ARR = array('a', 'b');但是,在5.6中有一个奇怪的问题:可以使用const声明常量数组,但不能定义()。PHP 7.0已经纠正了这一点。

#1


58  

UPDATE: this is possible in PHP 7 (reference)

更新:这在PHP 7中是可能的(引用)

// Works as of PHP 7
define('ANIMALS', array(
    'dog',
    'cat',
    'bird'
));
echo ANIMALS[1]; // outputs "cat"

ORIGINAL ANSWER

原来的答案

From php.net...

从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.

常数的值;只允许标量值和空值。标量值是整数、浮点数、字符串或布尔值。可以定义资源常量,但是不建议这样做,并且可能导致不可预测的行为。

$months = array("January,"February",...) will be just fine.

$months = array(“一月”、“二月”、“…”)就可以了。

#2


77  

you can use const for that purpose since PHP 5.6 (via nikic).

您可以为此目的使用const,因为PHP 5.6(通过nikic)。

const months = ["January", "February"];
var_dump("January" === months[0]);

#3


29  

You can put arrays inside constants with a hack:

你可以把数组放在常量里面

define('MONTHS', serialize(array('January', 'February' ...)));

But then you have to unserialize() that constant value when needed and I guess this isn't really that useful.

但是当需要时,你必须不序列化()那个常量值,我想这并不是很有用。

As an alternative, define multiple constants:

另外,定义多个常量:

define('MONTH_1', 'January');
define('MONTH_2', 'February');
...

And use constant() function to look up the value:

使用constant()函数查找值:

echo constant('MONTH_'.$month);

#4


11  

No, you can't. See PHP: Syntax - Manual

不,你不能。参见PHP:语法-手册

Only scalar data (boolean, integer, float and string) can be contained in constants. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.

只有标量数据(布尔数据、整数数据、浮点数据和字符串)可以包含在常量中。可以将常量定义为资源,但应该避免它,因为它可能导致意外结果。

#5


9  

You can use JSON format to keep array in string and then assign this string to constant.

可以使用JSON格式将数组保存在字符串中,然后将该字符串赋值为常量。

$months = array("January","February","March");
define('MONTHS', json_encode($months));

When you want to use it:

当你想使用它:

$months = json_decode(MONTHS);

#6


6  

If you must have a constant, how about using a a delimited string and exploding into an array?

如果您必须有一个常量,那么使用一个带分隔符的字符串并爆炸成一个数组怎么样?

define("MONTHS", "January;February;March");
$months = explode(";",MONTHS);

#7


6  

As of PHP 5.6, it is possible to declare constant arrays. The linked documentation uses the example const ARR = ['a', 'b'];. You could also do const ARR = array('a', 'b');. However, in 5.6 there is an odd quirk: you can declare constant arrays using const, but not define(). This has been corrected in PHP 7.0.

在PHP 5.6中,可以声明常量数组。链接的文档使用示例const ARR = ['a', 'b'];还可以使用const ARR = array('a', 'b');但是,在5.6中有一个奇怪的问题:可以使用const声明常量数组,但不能定义()。PHP 7.0已经纠正了这一点。