function_exists

Returns true when a function with the given name has been defined and is available.

Signature
function_exists(string $function): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$functionstringThe function name to look for. Matching is case-insensitive, but namespaced functions must be given with their full namespace path.

Return value

booltrue if the named function is defined, false otherwise.

Errors

Throws a TypeError if the argument is not a string or cannot be coerced to one.

Changelog

  • PHP 8.0Functions listed in the disable_functions INI setting are removed from the function table at compile time, so function_exists() now returns false for them instead of true.

function_exists() checks PHP's function table for a name, covering both internal functions and ones your code declared. It is the standard guard for feature detection.

The two everyday uses

First, polyfills: wrap a fallback definition in if (!function_exists('...')) so the file stays safe to include twice and so a newer PHP that ships the real function always wins. Second, optional extensions: an image resizer that only works when GD is compiled in, a cache layer that prefers APCu when available.

What it does not tell you

It matches functions only. Language constructs such as echo, isset, empty, include and list are compiled inline and always report false — that surprises people checking for empty. Methods are not functions either; use method_exists() or is_callable() for those. And a name existing does not guarantee the function will work: some extensions register names that fail at runtime depending on how they were built.

Namespaces

The lookup uses the fully qualified name. Inside namespace App;, checking 'helper' looks in the global namespace, not App\helper. Prefix with __NAMESPACE__ . '\\' when you mean the current one.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Guarding a polyfill so a newer PHP version's native implementation takes precedence
  • Detecting whether an optional extension such as GD, APCu or intl is available before using it
  • Checking for a function added in a later PHP release before calling it in a library that supports several versions
  • Failing early with a clear message when a required function is missing from the host

Frequently asked questions

What is the difference between function_exists and is_callable?

function_exists() only looks up plain function names in the function table, while is_callable() also accepts closures, [$object, 'method'] pairs and invokable objects and checks they can be called from the current scope. Use is_callable() when the value might be any kind of callable.

Why does function_exists('empty') return false?

empty, isset, echo, include and list are language constructs compiled directly into opcodes, not functions, so they never appear in the function table. There is no runtime check for them.

Is function_exists case-sensitive?

No, function names in PHP are case-insensitive, so function_exists('STRLEN') and function_exists('strlen') both return true. The namespace part of the name is case-insensitive too, but it must be present.

More examples

The polyfill pattern

Prints 13, then true twice. The second true shows that lookups are case-insensitive, which is why a polyfill guard cannot be defeated by a differently-cased duplicate definition elsewhere.

Example · php
<?php
if (!function_exists('array_sum_positive')) {
    function array_sum_positive(array $numbers): int|float {
        return array_sum(array_filter($numbers, fn($n) => $n > 0));
    }
}

echo array_sum_positive([3, -4, 10, -1]), "\n";
var_dump(function_exists('array_sum_positive'));
var_dump(function_exists('ARRAY_SUM_POSITIVE'));

Namespaces need the full path

Prints true, true, false, true. The bare name 'helper' fails because the string is resolved against the global namespace regardless of where the call appears, so build the name from __NAMESPACE__ when checking your own functions.

Example · php
<?php
namespace App;

function helper(): string { return 'ok'; }

var_dump(function_exists('App\helper'));
var_dump(function_exists('APP\HELPER'));
var_dump(function_exists('helper'));
var_dump(function_exists(__NAMESPACE__ . '\helper'));

function_exists versus is_callable and method_exists

Prints true, false, true, false, true. function_exists() only knows about functions, so the Class::method string fails; method_exists() confirms the method is declared; is_callable() additionally checks it can be called from here, which a non-static method reached statically cannot.

Example · php
<?php
class Mailer { public function send(): bool { return true; } }

var_dump(function_exists('strlen'));
var_dump(function_exists('Mailer::send'));
var_dump(method_exists('Mailer', 'send'));
var_dump(is_callable(['Mailer', 'send']));
var_dump(is_callable([new Mailer(), 'send']));

Discussion

  • Be the first to comment on this lesson.