How can I check if the function my_function
already exists in PHP?
Use an if Conditional Statement The code in the brackets will execute if the function is defined. If instead you just test the function without using the window object, for example as if(aFunctionName) , JavaScript will throw a ReferenceErorr if the function doesn't exist.
Use the PHP method_exists() function to check if an object or a class has a specified method.
2. Which of the following is not a built-in function in php ? Explanation: fclosed() is not a built-in function in php.
The is_callable() function checks whether the contents of a variable can be called as a function or not. This function returns true (1) if the variable is callable, otherwise it returns false/nothing.
Using function_exists
:
if(function_exists('my_function')){ // my_function is defined }
http://php.net/manual/en/function.function-exists.php
<?php if (!function_exists('myfunction')) { function myfunction() { //write function statements } } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With