php 是否可以将数组声明为常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3803349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to declare an array as constant
提问by mrN
We can define a constant like
我们可以定义一个常量,如
define("aconstant','avalue');
Can't we define array in this fashion like below ?
我们不能像下面这样以这种方式定义数组吗?
define("months",array("January", "February", ---);
回答by masakielastic
回答by Dejan Marjanovic
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("January,"February",...)
会没事的。
回答by Sim
You can put arrays inside constants with a hack:
您可以使用 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.
但是unserialize()
,在需要时您必须使用该恒定值,我想这并不是那么有用。
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);
回答by Dennis Haarbrink
No, you can't. See PHP: Syntax - Manual
不,你不能。请参阅PHP:语法 - 手册
Only scalar data(boolean, integer, floatand 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.
仅标量数据(布尔,整数,浮点和串)可被包含在常量。可以将常量定义为资源,但应避免这样做,因为它会导致意外结果。
回答by Exit196
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);
回答by jfmercer
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 correctedin PHP 7.0.
从 PHP 5.6 开始,可以声明常量数组。链接的文档使用示例const ARR = ['a', 'b'];
。你也可以这样做const ARR = array('a', 'b');
。但是,在 5.6 中有一个奇怪的怪癖:您可以使用 声明常量数组const
,但不能使用define()
。这已在 PHP 7.0 中得到纠正。
回答by Mark A. Tagliaferro
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);