Custom Function

A custom function, or user-defined function, is a function declared by the developer, as opposed to a native PHP function provided by the engine or an extension. Custom functions encapsulate reusable logic under a named callable.

Functions are declared with the function keyword and may be defined in any scope. Unlike many languages, PHP functions are global by default once declared: a function defined inside a conditional block or another function is still globally accessible after that point is reached at runtime.

Custom functions can be:

  • Regular functions: function myFunction() {}

  • Anonymous functions (closures): $fn = function() {}

  • Arrow functions: $fn = fn() => expression

  • Static methods: functions bound to a class

PHP also supports first-class callables, since PHP 8.1, and can call custom functions by name stored in a variable, as a dynamic calls.

<?php

// Regular custom function
function clamp(int $value, int $min, int $max): int {
    return max($min, min($max, $value));
}

echo clamp(150, 0, 100); // 100

// Anonymous function stored in a variable
$double = function(int $n): int {
    return $n * 2;
};

echo $double(5); // 10

// Arrow function
$triple = fn(int $n): int => $n * 3;
echo $triple(5); // 15

// First-class callable syntax (PHP 8.1)
$fn = clamp(...);
echo $fn(42, 0, 50); // 42

?>

Documentation

See also User-defined functions - PHP Manual.

Related : Functions, Anonymous Function, Arrow Functions, First Class Callable, Closure, PHP Native Function