我需要从字符串中提取PHP中常量的值。我的例子比我的话解释得更好:)
constants.php:
define('CONNECTION_FAILED', 'Connection Error');index.php:
include_once 'constants.php';
$success = $_GET['message']; //$success -> "CONNECTION_FAILED"现在,我要做的是显示常量的值,并在$success变量上显示名称。
你知道怎么做这件简单的事吗?
提前谢谢你
发布于 2017-08-26 11:55:19
是的,您可以使用常数函数获得计算名称的常量的值:
define('CONNECTION_FAILED', 'Connection Error');
$success = $_GET['message']; //$success -> 'CONNECTION_FAILED'
$message = constant($success); // 'Connection error'发布于 2017-08-26 11:55:12
您可以使用constant函数获取常量的值:
define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line就你的情况而言,它可以是:
echo constant($success);发布于 2017-08-26 11:57:14
参见constant http://dk.php.net/manual/en/function.constant.php ou can do constant($success)
https://stackoverflow.com/questions/45895028
复制相似问题