Home  >  Article  >  Backend Development  >  PHP 检查扩展库或函数是否可用的代码_php技巧

PHP 检查扩展库或函数是否可用的代码_php技巧

WBOY
WBOYOriginal
2016-05-17 09:26:541121browse

本文介绍的函数其实是PHP手册上本来就有的,但是由于这些函数独立性较强,查找不易,所以单独介绍一下,方便查阅。
1. 获取所有可用的模块 - get_loaded_extensions 该函数返回所有已经加载的(可用的)模块。
用法:

复制代码 代码如下:

print_r(get_loaded_extensions());

2. 获取指定模块的可用函数 - get_extension_funcs 该函数返回指定模块所有可用的函数。传入的参数(模块名称)必须是小写
用法:
复制代码 代码如下:

print_r(get_extension_funcs("gd"));

3. 获取所有已经定义的函数 - get_defined_functions 该函数返回所有已经定义的函数,包括内置函数和用户自定义函数。
用法:
复制代码 代码如下:

function myrow($id, $data){
return " $id $data \n";
}
$arr = get_defined_functions();
print_r($arr);

输出:
复制代码 代码如下:

Array
(
[internal] => Array
(
[0] => zend_version
[1] => func_num_args
[2] => func_get_arg
[3] => func_get_args
[4] => strlen
[5] => strcmp
[6] => strncmp
...
[750] => bcscale
[751] => bccomp
)
[user] => Array
(
[0] => myrow
)
)

其中 $arr["internal"] 是内置函数, $arr["user"] 是用户自定义函数。
4. 检查指定函数是否存在 - function_exists 该函数返回指定函数是否已经定义。
用法:
复制代码 代码如下:

if (function_exists('imap_open')) {
echo "IMAP functions are available.
\n";
} else {
echo "IMAP functions are not available.
\n";
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn