Functions group reusable logic. PHP functions are global by default unless defined inside classes or namespaces.
Definition and call
function greet(string $name): string {
return "Hello, {$name}";
}
echo greet('Ada');
Type hints (PHP 7+)
Parameter and return types catch mistakes early. Use declare(strict_types=1); at the top of files in strict codebases.
Arrow functions (PHP 7.4+)
$double = fn (int $n): int => $n * 2;
Arrow functions capture variables from the parent scope automatically by value.
Self-check
- Where does execution jump after a
return? - When are arrow functions preferable to full function syntax?