basename( ) function in PHP

PHPProgrammingServer Side Programming

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.

 Live Demo

<?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.

 Live Demo

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

Output

new
raja
Published on 03-Oct-2018 08:17:24
Advertisements