PHP 常量包含数组? [英] PHP Constants Containing Arrays?
本文介绍了PHP 常量包含数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
失败:
define('DEFAULT_ROLES', array('guy', 'development team'));
显然,常量不能容纳数组.解决这个问题的最佳方法是什么?
Apparently, constants can't hold arrays. What is the best way to get around this?
define('DEFAULT_ROLES', 'guy|development team');
//...
$default = explode('|', DEFAULT_ROLES);
这似乎是不必要的努力.
This seems like unnecessary effort.
推荐答案
注意:虽然这是公认的答案,但值得注意的是,在 PHP 5.6+ 中,您可以拥有 const 数组 - 请参阅下面的 Andrea Faulds 的回答.
您也可以序列化您的数组,然后将其放入常量中:
You can also serialize your array and then put it into the constant:
# define constant, serialize array
define ("FRUITS", serialize (array ("apple", "cherry", "banana")));
# use it
$my_fruits = unserialize (FRUITS);
这篇关于PHP 常量包含数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文