如何将在单个php文件中定义的所有常量获取到数组中
这是一个php文件,其中定义了常量
我想把这个页面中的所有常量都放到一个数组中
我该怎么做这份工作
<?php
/**
* hello wolrd
----------------------------------------
*/
define('HELLO_WORLD','hello world');
define('GOOD_BOOD','
very good
');
define(
'JY_CREDITS_HTML',
'
<p>hello wrold</p>
<div class="good">
<span>world</span>
</div>
'
);
// define('META_KEY','Wat adidi');
define('XXX',"xxx")
/*
hello world
*/
?>
结果应该是
<?php
$result=array(
'HELLO_WORLD' => 'hello wrold',
'GOOD_BOOD' => 'very good',
'JY_CREDITS_HTML' =>'....',
'XXX'=>'xxx'
);
?>
print_r( get_defined_constants(true)['user'] );
<?php
print_r(get_defined_constants(true));
?>
include('hello_world.php');
$constants = get_defined_constants();
在与常量相同的脚本中工作时,不需要Include((。
<?php
$user_def_cnst=get_defined_constants(true)['user'];
print_r($user_def_cnst);
?>
您可以始终使用以下内容:-
define('HELLO_WORLD','hello world');
define('GOOD_BOOD','
very good
');
define(
'JY_CREDITS_HTML',
'
<p>hello wrold</p>
<div class="good">
<span>world</span>
</div>
'
);
define('XXX',"xxx");
$result = get_defined_constants(true);
if(isset($result['user'])){
$result = $result['user']; //just incase there are no user constants set.
}
var_dump($result);
输出:-
array (size=4)
'HELLO_WORLD' => string 'hello world' (length=11)
'GOOD_BOOD' => string '
very good
' (length=17)
'JY_CREDITS_HTML' => string '
<p>hello wrold</p>
<div class="good">
<span>world</span>
</div>
' (length=92)
'XXX' => string 'xxx' (length=3)
请参阅此处http://www.php.net/manual/en/function.get-defined-constants.php