Functions in PHP

From the PHP cheat sheet · Functions & Closures · verified Jul 2026

Functions

Function declaration and parameters

php
// Basic function
function greet($name) {
    return "Hello, $name!";
}

// Type hints and return types
function add(int $a, int $b): int {
    return $a + $b;
}

// Default parameters
function connect($host = 'localhost', $port = 3306) {
    // connection logic
}

// Variadic functions
function sum(...$numbers): float {
    return array_sum($numbers);
}

// Named arguments (PHP 8+)
connect(port: 8080, host: 'example.com');
💡 Named arguments improve readability
⚡ Variadic functions accept unlimited arguments
📌 Pass by reference with & to modify variables
🔥 Type hints catch errors early

More PHP tasks

Back to the full PHP cheat sheet