是否可以在 wp-config.php 中定义一个数组
Is it possible to define an array in wp-config.php
wp-config.php
$array = array("test1","test2");
define("TEST", $array);
testFile.php
$array = TEST;
这可能吗?上面的代码只是将 $array 设置为 "TEST"。或者有没有办法通过字符串从 wp-config.php 中检索值,例如:
$array_first_value = wp_get_config("array_first_value");
数组值不能与 define
一起使用。您只能使用标量(int、float、string、bool)或 null。
您可以序列化数组以在常量中使用,并在以后需要时反序列化它。
因此,在 wp-config.php 中,您可以执行以下操作:
$array = array( "testvalue1", "testvalue2" );
define( "TEST", serialize($array) );
然后在其他文件中:
$array = unserialize(TEST);
但是,常量并不意味着保存数组。因此,您可能需要重新考虑对常量数组的需求。
wp-config.php
$array = array("test1","test2");
define("TEST", $array);
testFile.php
$array = TEST;
这可能吗?上面的代码只是将 $array 设置为 "TEST"。或者有没有办法通过字符串从 wp-config.php 中检索值,例如:
$array_first_value = wp_get_config("array_first_value");
数组值不能与 define
一起使用。您只能使用标量(int、float、string、bool)或 null。
您可以序列化数组以在常量中使用,并在以后需要时反序列化它。
因此,在 wp-config.php 中,您可以执行以下操作:
$array = array( "testvalue1", "testvalue2" );
define( "TEST", serialize($array) );
然后在其他文件中:
$array = unserialize(TEST);
但是,常量并不意味着保存数组。因此,您可能需要重新考虑对常量数组的需求。