Home > Article > Backend Development > Understanding the usage of php variable functions
function func() { return 'hello,world!'; } $myFunction = 'func';
When we create a custom function and understand the usage of variable functions, in order to ensure that the function called by the program exists, we often first Use function_exists to determine whether the function exists.
The same method_exists can be used to detect whether a class method exists.
if (function_exists($myFunction )){ echo 'exists'; }
Whether the class is defined, class_exists can be used.
class MyClass{ } // 使用前检查类是否存在 if (class_exists('MyClass')) { $myclass = new MyClass(); }
There are many such checking methods in PHP, such as whether the file existsfile_existsetc.
$filename = 'test.txt'; if (!file_exists($filename)) { echo $filename . ' not exists.'; }
The above is the detailed content of Understanding the usage of php variable functions. For more information, please follow other related articles on the PHP Chinese website!