Computer >> Computer tutorials >  >> Programming >> PHP

basename( ) function in PHP


The basename() function gets the the filename component of a path. The file path is set as a parameter.

Syntax

basename(file_path, suffix)

Parameters

  • file_path − Set the path of file or directory to be checked. Required.

  • suffix − Set the extension of the file. Optional.

Return

The basename() function returns the basename of the file.

Example

The following is an example that checks for file “new.php” and returns the basename with the extension since we added the suffix parameter.

<?php
   $file_path = "/backup/myfiles/new.php";
   // displays name of the file with extension
   echo basename($file_path);
?>

Output

new.php

Example

Let us see another example that checks the file and display the basename without the extension.

<?php
   $file_path = "/backup/myfiles/new.php";
   // displays name of the file without extension
   echo basename($file_path,".php");
?>

Output

new