What is the basename() function in PHP?
The basename() function is a built-in function in PHP. Given the path of any file, this method can remove the entire path and return only the filename.
Syntax:
string basename ( $path , $suffix )
Parameters
$path: String value that specifies the path of a file.
$suffix: An optional parameter used if a user does not want to return the extension of the file.
Return value
This function returns just the name of the file, removing the path.
Code
Here is a coding example of the basename function.
<?php$path = "user/home/documents/Hello.txt";// using the function on the specified pathecho basename($path);?>
<?php$path = "user/home/documents/Hello.txt";// using suffix parameterecho basename($path, ".txt");?>
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved