function_exists() checks whether a named function is defined. It is useful for optional integrations or compatibility code, but normal application code should not need repeated checks for functions it requires.
<?php
if (function_exists('mb_strlen')) {
echo mb_strlen('Hello');
}
?>
if(function_exists(is_int)){
echo " Yes this function is available to use ";
}else{
echo " Sorry , this is not available to use " ;
}
In the above code we have used is_int() which is a built in function in PHP, so we will get the output as this.
Yes this function is available to use
<?php
function my_function($var1,$var2,$var3){
$var=$var1+$var2+$var3;
return $var;
}
echo my_function(5,6,8); // Output 19
if(function_exists(is_int)){
echo " <br>Yes <i>my_function</i> is available to use ";
}else{
echo " <br>Sorry , <i>my_function</i> is not available to use " ;
}
?>
Output
19
Yes my_function is available to use
code>if (function_exists('mysqli_connect')) {
echo "mysqli is installed";
}else{
echo " Enable Mysqli support in your PHP installation ";
}code>
function myFunction() {
echo "Function exists!";
}
if (function_exists('myFunction')) {
myFunction(); // Output: Function exists!
} else {
echo "Function not found!";
}
if (function_exists('nonexistentFunction')) {
nonexistentFunction(); // Avoids fatal error
} else {
echo "Function does not exist!";
}
### Description:namespace MyNamespace;
function customFunc() {
echo "Namespace Function";
}
if (function_exists('MyNamespace\\customFunc')) {
customFunc(); // Output: Namespace Function
}
This way, the `function_exists()` page can be improved by providing more real-life examples and handling different PHP contexts such as namespaces and custom functions.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.