Basename in PHP | PHP basename() Function

Published:Dec 1, 202313:23
0

Introduction

PHP has a operate referred to as basename that helps to fetch the filename current on the specified path. The operate returns and prints the filename or the listing path handed in arguments. The process additionally shows the script identify if the basename used is $0. Programmers use this operate to verify the recordsdata current on the specified location or use this system circulate filenames.

Therefore, the programmers use the basename operate in PHP to seek out the recordsdata current at any location and get them printed on the console. The basename operate is like another built-in operate or user-defined operate in PHP, resembling string capabilities, math capabilities, numeric capabilities, date capabilities, and plenty of extra.

Basename Perform in PHP

The basename is an in-built operate in PHP, which returns the file identify current on the path supplied as an argument.

Syntax:

Beneath is the syntax to make use of the Basename operate in PHP.

String basename($path, $suffix)

The operate has two parameters, i.e., path and suffix.

  • $path: Path is a compulsory parameter and s of String kind. This parameter specifies the trail at which the file is to find.
  • $suffix: Suffix is an elective parameter that hides the file extension if the file has a suffix.

The operate returns the file basename, which is current on the path handed as $path within the basename parameter.

Examples of Basename in PHP

Example1: The code prints the identify of the file current at a specified location with out utilizing the elective parameter $suffix.

<?php

// Save the trail within the $path variable

$path = “/ExampleProject1/Example1.php”;

// basename operate finds the identify of the file current at $path and saves it in //$fileName variable

$fileName = basename($path);

// Prints the filename

Echo $fileName;

Echo “n”

?>

Output:

Example1.php

Right here, solely the $path parameter is handed with the basename operate name, so the entire file identify will get printed.

Example2: The code prints the file’s identify on the specified location when the elective parameter $suffix is handed.

<?php

// Save the trail within the $path variable

$path = “/ExampleProject1/Example1.php”;

// Utilizing the $suffix parameter in order that the output comprises solely the filename, with out //extension

$fileName = basename($path, “.php”);

Echo $fileName;

?>

Output:

Example1

Right here, $suffix is handed with a operate name, so solely the file identify will get printed with out extension.

Example3: The code prints the identify of the file current on the specified location with each variations of the basename operate.

<?php

// Save the trail within the $path variable

$path = “/Project1/team1/FirstProgram.php”;

// Utilizing the $path parameter with basename operate

$fileName = basename($path);

Echo $fileName;

Echo “n” // It takes the cursor to subsequent line

// Utilizing the $path parameter with basename operate

$fileName = basename($path, “.php”);

Echo $fileName;

?>

Output:

FirstProgram.php

FirstProgram

When solely the $path is handed as a parameter, the entire filename will get printed. When the $suffix .php is handed with a operate name, the .php will get hidden and solely filename will get printed.

Errors and Exception in Basename in PHP

It's certainly essential to know the functioning and utilization of basename operate in PHP, however it's also essential to know the exceptions to make use of the method precisely:

Basename operate in PHP

Can't establish the $path worth given as ‘..’; nonetheless, it could actually recognise the one dot ‘.’. Double beads ‘..’ are solely utilized in Linux that transfer the placement from the present listing to the earlier listing. As double dots ‘..’ is utilized in Linux solely, so basename in PHP solely recognises the one drop ‘.’.

Basename operate in PHP

Makes use of the $path handed as string and isn't conscious of the file system. It implies that the method solely works on the way in which handed as a parameter no matter the filesystem kind. The file system might be Mac, Linux, and Home windows. As every of those file programs has a special format, and basename operate can't recognise the kind of filesystem used. Subsequently, the output can get incorrect. Therefore, it is suggested to go parameters within the basename operate.

Slashes are used because the separator within the listing path or to separate the folders. Home windows platform can recognise each backslash () and forward-slash (/) as a separator within the listing path, whereas in different environments, solely forward-slash (/) will get used. So, we should always use slash cautiously whereas utilizing the basename operate in PHP.

Conclusion

This was all in regards to the basename operate in PHP. We hope you will have a deep understanding of PHP’s basename, utilization, syntax, and totally different exceptions. The exceptions must be thought of earlier than utilizing the basename operate in order that the proper output might be anticipated from it.

Should you’re to be taught extra about full-stack software program improvement, try upGrad & IIIT-B’s PG Diploma in Full-stack Software program Growth, which is designed for working professionals and affords 500+ hours of rigorous coaching, 9+ tasks, and assignments, IIIT-B Alumni standing, sensible hands-on capstone tasks & job help with high corporations.

Grow to be a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT LEARN MORE@UPGRAD



To stay updated with the latest bollywood news, follow us on Instagram and Twitter and visit Socially Keeda, which is updated daily.

sociallykeeda profile photo
sociallykeeda

SociallyKeeda: Latest News and events across the globe, providing information on the topics including Sports, Entertainment, India and world news.