Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

basename
php

What is the basename() function in PHP?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The basename() function is a built-in function in PHP. Given the path of any file, this method can remove the entire path and return only the filename.

Syntax:

string basename ( $path , $suffix )

Parameters

$path: String value that specifies the path of a file.

$suffix: An optional parameter used if a user does not want to return the extension of the file.

Return value

This function returns just the name of the file, removing the path.

Code

Here is a coding example of the basename function.

<?php
$path = "user/home/documents/Hello.txt";
// using the function on the specified path
echo basename($path);
?>
Code example with the file extension included
<?php
$path = "user/home/documents/Hello.txt";
// using suffix parameter
echo basename($path, ".txt");
?>
Code example without the extension

RELATED TAGS

basename
php

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring