Home  >  Article  >  Backend Development  >  How to check if a PHP function exists?

How to check if a PHP function exists?

WBOY
WBOYOriginal
2024-04-11 09:48:011130browse

In PHP, use the function_exists() function to check whether the function exists: it receives the function name parameter (string). Returns a Boolean value: true if the function exists, false if it does not exist.

如何检查 PHP 函数是否存在?

#How to check if a PHP function exists?

In PHP, you can use the function_exists() function to check if a function exists. This function receives one parameter, the function name (in string form), and returns a Boolean value: true means the function exists, false means it does not exist.

Syntax:

bool function_exists ( string $function_name )

Parameters:

  • $function_name - to be checked Function name (string)

Actual case:

<?php

// 检查是否存在 my_function 函数
if (function_exists('my_function')) {
    echo 'my_function 存在';
} else {
    echo 'my_function 不存在';
}

Output:

my_function 不存在

Because The my_function function has not been defined yet, so the output will be "my_function does not exist".

Note:

  • Function names must be lowercase.
  • If you use the __autoload() function to automatically load a function, you need to call this function before checking whether the function exists.
  • You can also use the is_callable() function to check whether the function exists and is callable.

The above is the detailed content of How to check if a PHP function exists?. For more information, please follow other related articles on the PHP Chinese website!

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