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.

 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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 24-Jun-2020

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements