Define a function with the function keyword. Parameters can have default values and type hints:

<?php
function greet(string $name, string $greeting = "Hello"): string {
    return "$greeting, $name!";
}

echo greet("Alice");            // Hello, Alice!
echo greet("Bob", "Hi");        // Hi, Bob!

Useful built-in string functions:

<?php
strlen("hello");                // 5
strtoupper("hello");            // "HELLO"
str_replace("world","PHP","hello world");  // "hello PHP"
trim("  padded  ");             // "padded"
explode(",", "a,b,c");          // ["a","b","c"]
implode("-", ["a","b","c"]);    // "a-b-c"

Anonymous functions (closures) can be passed as arguments or stored in variables:

<?php
$double = function(int $n): int { return $n * 2; };
echo $double(5);   // 10

$numbers = [1, 2, 3, 4, 5];
$evens   = array_filter($numbers, fn($n) => $n % 2 === 0);
$doubled = array_map(fn($n) => $n * 2, $numbers);