PHP basename Function With Examples

The PHP basename is a function which returns the filename of a file path directory.

In this article, we will talk about PHP basename in a detailed explanation as well as example programs that could be helpful to your project development. This topic is a continuation of the previous article, entitled How To Find The Array Length PHP.

What is the basename in PHP?

In PHP, basename is a built-in function which mainly used to return the base name of the given file on the fixed condition where the desired file path is given inside as a parameter in the base name function.

Syntax

basename(path, suffix)

Parameter values

Parameter Description
pathThe path parameter is required and specifies the file path directory.
suffixThe suffix parameter is optional, once the filename has a file extension (.php) these file path will be removed.

Example

The following program will display the name of the file path with an extension.

<?php
     $file_path_inventory = "/inventory/price/add.php";
     // displays name of the file with extension
     echo basename($file_path_inventory);
?>

Output

add.php

Example

The following program will display the name of the file path without an extension.

<?php
     $file_path_inventory = "/inventory/price/add.php";
     // displays name of the file with extension
     echo basename($file_path_inventory,".php");
?>

Output

add

Errors and Exceptions

  • The basename function does not acknowledge a path with symbols such as (‘..’ .).
  • The basename is a function which only operates on the input string which is provided by the user and does not aware of the existing filesystem.
  • Both slashes such as forward and backslash is used as separator on directory of character on windows platform, While on other platforms such as Linux and Mac OS you can only use the forward slash which is hard for the basename function to read.

What is the basename of a file?

The basename of a file is a final rightmost segment of a file path components directory which is usually called a (file) or sometimes they call it a directory name.

What is basename path?

The basename PHP path is a function that takes the pointed pathname to the exact path and returns the pointer of a final component of the pathname and deleting any kind of trailing ‘/’ characters.

Further, once the string characters was consists entirely of a backslash (‘/’) character, then the basename function in PHP will return the pointer to basename string and path string.

What is basename used for?

The basename is a function used to return the filename of a path directory.

What basename command is used for?

The basename command is used to read a string path and string suffix, write all the remaining base file name into standard output, and delete any kind of prefix with a forward slash (/) and any spell out suffix parameter.

Summary

In summary, you have learned about PHP basename. This article also discussed what is the basename in PHP, what is the basename of a file, what is basename path, what is basename used for, and what the basename command is used for.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment