Basename in PHP | PHP basename() Function

Published:Nov 23, 202300:25
Basename in PHP | PHP basename() Function

Download Now ????????


PHP has a function known as basename that helps to fetch the filename present on the desired path. The function returns and prints the filename or the itemizing path handed in arguments. The method moreover exhibits the script determine if the basename used is $0. Programmers use this function to confirm the recordsdata present on the desired location or use this method flow into filenames. - Commercial- Subsequently, the programmers use the basename function in PHP to hunt out the recordsdata present at any location and get them printed on the console. The basename function is like one other built-in function or user-defined function in PHP, resembling string capabilities, math capabilities, numeric capabilities, date capabilities, and loads of further.

Basename Carry out in PHP

The basename is an in-built function in PHP, which returns the file determine present on the trail equipped as an argument. - Commercial- Syntax: Beneath is the syntax to utilize the Basename function in PHP. String basename($path, $suffix) - Commercial- The function has two parameters, i.e., path and suffix.
  • $path: Path is a obligatory parameter and s of String type. This parameter specifies the path at which the file is to search out.
  • $suffix: Suffix is an elective parameter that hides the file extension if the file has a suffix.
The function returns the file basename, which is present on the trail handed as $path inside the basename parameter. - Commercial-

Examples of Basename in PHP

Example1: The code prints the determine of the file present at a specified location with out using the elective parameter $suffix. <?php // Save the path inside the $path variable $path = “/ExampleProject1/Example1.php”; // basename function finds the determine of the file present at $path and saves it in //$fileName variable - Commercial- $fileName = basename($path); // Prints the filename Echo $fileName; Echo “n” ?> Output: Example1.php Proper here, solely the $path parameter is handed with the basename function identify, so all the file determine will get printed. Example2: The code prints the file’s determine on the desired location when the elective parameter $suffix is handed. <?php // Save the path inside the $path variable $path = “/ExampleProject1/Example1.php”; // Using the $suffix parameter so that the output contains solely the filename, with out //extension $fileName = basename($path, “.php”); Echo $fileName; ?> Output: Example1 Proper here, $suffix is handed with a function identify, so solely the file determine will get printed with out extension. Example3: The code prints the determine of the file present on the desired location with every variations of the basename function. <?php // Save the path inside the $path variable $path = “/Project1/team1/FirstProgram.php”; // Using the $path parameter with basename function $fileName = basename($path); Echo $fileName; Echo “n” // It takes the cursor to subsequent line // Using the $path parameter with basename function $fileName = basename($path, “.php”); Echo $fileName; ?> Output: FirstProgram.php FirstProgram When solely the $path is handed as a parameter, all the filename will get printed. When the $suffix .php is handed with a function identify, the .php will get hidden and solely filename will get printed.

Errors and Exception in Basename in PHP

It’s definitely important to know the functioning and utilization of basename function in PHP, nonetheless it’s additionally important to know the exceptions to utilize the tactic exactly:

Basename function in PHP

Can’t set up the $path value given as ‘..’; nonetheless, it may really recognise the one dot ‘.’. Double beads ‘..’ are solely utilized in Linux that switch the location from the current itemizing to the sooner itemizing. As double dots ‘..’ is utilized in Linux solely, so basename in PHP solely recognises the one drop ‘.’.

Basename function in PHP

Makes use of the $path handed as string and isn’t aware of the file system. It implies that the tactic solely works on the way in which through which handed as a parameter irrespective of the filesystem type. The file system is perhaps Mac, Linux, and Residence home windows. As each of these file applications has a particular format, and basename function can’t recognise the form of filesystem used. Subsequently, the output can get incorrect. Subsequently, it's prompt to go parameters inside the basename function. Slashes are used as a result of the separator inside the itemizing path or to separate the folders. Residence home windows platform can recognise every backslash () and forward-slash (/) as a separator inside the itemizing path, whereas in numerous environments, solely forward-slash (/) will get used. So, we should always all the time use slash cautiously whereas using the basename function in PHP.


This was all regarding the basename function in PHP. We hope you should have a deep understanding of PHP’s basename, utilization, syntax, and completely totally different exceptions. The exceptions have to be considered sooner than using the basename function so that the correct output is perhaps anticipated from it. Do you have to’re to be taught further about full-stack software program program enchancment, 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 teaching, 9+ duties, and assignments, IIIT-B Alumni standing, wise hands-on capstone duties & job assist with excessive companies.

Develop to be a Full Stack Developer


Disclaimer: We at request you to take a look at movement footage on our readers solely with cinemas and Amazon Prime Video, Netflix, Hotstar and any official digital streaming corporations. Don’t use the pyreated website to acquire or view on-line.

Download & Watch Online

Download Server 1 Download Server 2

For more on news and current affairs, please visit Socially Trend.








Socially Trend profile photo
Socially Trend is an Entertainment News Portal. It shares the Latest Entertainment News and Updates we provide the latest updates on various topics globe.

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies.