PHP basename( ) Function
Last Updated :
26 Jun, 2023
Improve
The basename() function in PHP is an inbuilt function which is used to return the base name of a file if the path of the file is provided as a parameter to the basename() function.
Syntax:
php
Output:
php
Output:
string basename ( $path , $suffix )Parameters: The basename() function in PHP accepts two parameters which are path and suffix.
- $path: This parameter is of string type and is mandatory. It specifies the path of the file.
- $suffix: It is an optional parameter which hides the extension of a file if it ends with a suffix.
- The basename() function doesn't recognise path components such as '..' .
- The basename() function operates on the input string provided by the user and is unaware of the actual filesystem.
- Both slashes, forward slash (/) and backslash (\) are used as directory separator character on a windows platform whereas it is just a forward slash (/) in other environments.
Input : $path = "user01/home/documents/geeksforgeeks.php", Output : geeksforgeeks.php Input : $path = "user01/home/documents/geeksforgeeks.php", $suffix = ".php" Output : geeksforgeeksBelow programs illustrate the basename() function: Program 1:
<?php
$path = "user01/home/documents/geeksforgeeks.php";
// basename() function to show
// filename along with extension
echo basename($path);
?>
geeksforgeeks.phpProgram 2:
<?php
$path = "user01/home/documents/geeksforgeeks.php";
// basename() function to show the
// filename while hiding the extension
echo basename($path, ".php");
?>
geeksforgeeksReference: http://php.net/manual/en/function.basename.php