PHP 要秀出 或 使用 動態 Constant / Define 值, 要如何使用呢? ex: 下述範例是動態變數, 但是動態 Constant 要如何使用?
<?php $a = 'abc'; $b = 'a'; echo $$b; // 印出 abc ?>
PHP 秀出 或 使用 動態 Constant / Define 值
可以使用 constant() 來達成使用 Constant 的值, 範例如下:
<?php
define('PKEY1', 'abc');
define('PKEY2', 'def');
$i = 1;
$a = 'PKEY';
echo constant($a . $i); // abc
echo constant($a . ++$i); // def
?>