PHP常量包含有数组? [英] PHP Constants Containing Arrays?

查看:206
本文介绍了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+,你可以有常量数组 - 的见下文安德烈·福尔兹的回答

NOTE: while this is the top voted answer, it's worth noting that in PHP 5.6+ you can have const arrays - see Andrea Faulds' answer below.

您也可以序列化数组,然后把它放到常数:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆