PHP basename() Function: Description, Parameters, Return Value, and Examples

The PHP basename() function returns the filename component of a given path, optionally removing a specified suffix, and the article explains its parameters, return value, and provides multiple code examples with expected output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP basename() Function: Description, Parameters, Return Value, and Examples

The basename() function in PHP returns the base name of a given path, optionally removing a specified suffix.

Parameters

path : The full path to a file; both forward (/) and backslashes (\) are accepted on Windows, while only forward slashes are used on other systems.

suffix (optional): If the filename ends with this suffix, it will be stripped from the result.

Return value

Returns the base name component of $path as a string.

Example

<?php
echo "1) " . basename("/etc/sudoers.d", ".d") . PHP_EOL;
echo "2) " . basename("/etc/passwd") . PHP_EOL;
echo "3) " . basename("/etc/") . PHP_EOL;
echo "4) " . basename(".") . PHP_EOL;
echo "5) " . basename("/") . PHP_EOL;
?>

Output:

1) sudoers
2) passwd
3) etc
4) .
5)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendfunctionFile Pathbasename
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.