Constant and Define are constants in php, so below I will introduce a method to dynamically use the Constant and Define value methods. I hope it will be helpful to all students.
PHP wants to show or use dynamic Constant / Define values, how to use it? ex: The following example is a dynamic variable, but how to use dynamic Constant?
The code is as follows
代码如下 |
复制代码 |
$a = 'abc';
$b = 'a';
echo $$b; // 印出 abc
?>
|
|
Copy code
|
|
代码如下 |
复制代码 |
define('PKEY1', 'abc');
define('PKEY2', 'def');
$i = 1;
$a = 'PKEY';
echo constant($a . $i); // abc
echo constant($a . ++$i); // def
?>
|
$a = 'abc';
$b = 'a';
echo $$b; // print abc
?>
PHP uses dynamic Constant / Define values
You can use constant() to achieve the value of Constant, the example is as follows:
The code is as follows
|
Copy code
|
define('PKEY1', 'abc');
define('PKEY2', 'def');
$i = 1;
$a = 'PKEY';
echo constant($a . $i); // abc
echo constant($a . ++$i); // def |