basename()

basename() — Returns trailing name component of path.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Given a string containing the path to a file or directory, this function will return the trailing name component.

Signature

basename(string $path, string $suffix = ""): string

Parameters

ParameterTypeWhat it takes
$pathstringA path.
$suffixstringIf the name component ends in suffix this will also be cut off.

Return value

Returns the base name of the given path.

Example

A runnable snippet:

echo "1) ".basename("/etc/sudoers.d", ".d").PHP_EOL; echo "2) ".basename("/etc/sudoers.d").PHP_EOL; echo "3) ".basename("/etc/passwd").PHP_EOL; echo "4) ".basename("/etc/").PHP_EOL; echo "5) ".basename(".").PHP_EOL; echo "6) ".basename("/");

Output:

1) sudoers 2) sudoers.d 3) passwd 4) etc 5) . 6)

See also

dirname(), pathinfo()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page basename() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.