get_defined_constants() 函数是 PHP 中的内置函数,它以关联数组的形式返回当前定义的所有常量的名称及其值。
用法:
get_defined_constants(bool $categorize = false): array
参数:该函数接受如下所述的单个参数:
- $categorize: 此函数返回一个多维数组,其中包含第一维键中的类别和常量,其值将位于第二维中。
返回值:该函数以键和值的形式返回一个数组,即键名=>值。
示例 1:在下面的示例中,我们将调用get_defined_constants()函数并打印关联数组。此处,get_defined_constants 打印所有常量以及用户定义的常量。
PHP
<?php
define("MY_FIRST_CONSTANT",1);
print_r(get_defined_constants(true)) ;
?>输出:
Array (
[Core] => Array
(
[E_ERROR] => 1
[E_RECOVERABLE_ERROR] => 4096
[E_WARNING] => 2
[E_PARSE] => 4
[E_NOTICE] => 8
[E_STRICT] => 2048
[E_DEPRECATED] => 8192
[E_CORE_ERROR] => 16
[E_CORE_WARNING] => 32
[E_COMPILE_ERROR] => 64
[E_COMPILE_WARNING] => 128
[E_USER_ERROR] => 256
[E_USER_WARNING] => 512
[E_USER_NOTICE] => 1024
[E_USER_DEPRECATED] => 16384
[E_ALL] => 32767
[DEBUG_BACKTRACE_PROVIDE_OBJECT] => 1
[DEBUG_BACKTRACE_IGNORE_ARGS] => 2
)
[user] => Array
(
[MY_FIRST_CONSTANT] => 1
)
)
示例 2:在下面的示例中,我们将仅打印用户定义的常量用户get_defined_constants()函数。
PHP
<?php
define("MY_FIRST_CONSTANT",1);
print_r(get_defined_constants(true)["user"]) ;
?>输出:
Array ( [MY_FIRST_CONSTANT] => 1 )
参考: https://www.php.net/manual/en/function.get-defined-constants.php
相关用法
- PHP get_defined_vars()用法及代码示例
- PHP get_defined_functions()用法及代码示例
- PHP get_declared_interfaces()用法及代码示例
- PHP get_declared_classes()用法及代码示例
- PHP get_class_vars()用法及代码示例
- PHP get_parent_class()用法及代码示例
- PHP get_meta_tags()用法及代码示例
- PHP get_resource_id()用法及代码示例
- PHP get_resource_type()用法及代码示例
- PHP get_html_translation_table()用法及代码示例
- PHP get_browser()用法及代码示例
- PHP get_headers()用法及代码示例
- PHP get_class()用法及代码示例
- PHP get_class_methods()用法及代码示例
- PHP get_called_class()用法及代码示例
- PHP get_object_vars()用法及代码示例
- PHP get_included_files()用法及代码示例
- PHP get_current_user()用法及代码示例
- PHP get_mangled_object_vars()用法及代码示例
- PHP get_resources()用法及代码示例
- PHP get_include_path()用法及代码示例
- PHP get_loaded_extensions()用法及代码示例
- PHP getrandmax()用法及代码示例
- PHP getcwd()用法及代码示例
- PHP getdate()用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 PHP get_defined_constants() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
