Javatpoint Logo
Javatpoint Logo

PHP basename( ) Function

PHP functions are a self-centric block of statements that can be executed a number of times as per the developer's requirements.

PHP contains multiple inbuilt functions that can be used at times of need. In these functions, we don't have to give the definition of functions; all we have to do is declare the function and add parameters to use the same.

PHP inbuilt functions:

phpinfo() print() mysqli_connect() error_reporting()
error_log() array() copy() unlink()
date() time() strlen() strlen()

PHP Basename() function

The basename function is an inbuilt PHP function mainly used to return the base name of a given file on a certain condition when the path of the desired file is given as a parameter inside the base name function. i.e., it gives the trailing name of the path.

Syntax:

PHP basename() functions hold two parameters which are mandatory for the function to execute.

Parameter Description Required / Optional
$Path this parameter defines the path of the file or directory, this parameter is of string type and it is mandatory to provide this parameter in order to execute the function. Required
$Suffix it is a non-essential parameter that is used when a file contains a name extension that end with a suffix, this parameter hides the extension Optional

Example:

Exceptions:

  1. Components such as ' . . ' used to get inside a file are not recognized as a path by the basename( ) function
  2. The user's path is declared as a string; therefore, the basename( ) function does not recognize the actual file system used in operating systems.
  3. In windows, the directory system uses forward and backward slashes ( " / " , " \ " ) to move in and out of the file. These are called directory separators. Still, on another operating system like Linux, we can only use a forward slash.
  4. With the basename ( ) function, we can only have the base name of the declared directory whose spathe has been specified inside the basename ( ) function parameter. In order to get all the components of the file, we can use the pathinfo ( ) function, which is also an inbuilt PHP function.

Program 1:

Output:

myfirstPHP_file.php 

Here in this program, we have declared a variable $path and assigned the file's directory location whose base name we have to use. And in the echo statement, we have used the basename ( ) function with the path as a parameter, which will let the compiler display only the file name.

Program 2:

Output:

myfirstPHPfile

Here in this program, we have declared a variable $path and assigned the file's directory location whose base name we have to use. And in the echo statement, we have used the basename ( ) function with two parameters, path to assign the path of file and suffix, which will remove the file extension, and the compiler to display only the file name without the. PHP extension.

Program 3:

Output:

a ) myfirstPHPfile 
b ) mySecondPHPfile.php 
c ) en_US 
d ) langdata 
f ) Autoplay 
g ) software 
h ) D:
i ) .
j )
k ) Array ( 
                   [dirname] => D:\software\Autoplay\langdatan_US 
                   [basename] => mySecondPHPfile.php 
                   [extension] => php 
                   [filename] => mySecondPHPfile )

Here in this program we have declared a variable $path and assigned multiple values to it displaying all the forms in which the base name function can be utilised.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA