EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

basename in PHP

Home » Software Development » Software Development Tutorials » PHP Tutorial » basename in PHP

basename in PHP

Introduction to basename in PHP

The basename is a function that is used to get or retrieve the filename from a path. It simply prints the name of the file or directory passed or the existing script name is printed if we use basename $0. This function is used by the programmers to check the name of the existing script or file and to use it in the further flow of the program.

So, developers and programmers use this function in their application so that they can print the filename in the output console. In PHP, there are a lot of functions such as built-in, user-defined, date functions, string functions, numeric functions, math functions, etc. One of the built-in functions is the basename function where the user can get the current filename of the program and can be printed on the output console if required or else the user can just use it for the further flow of the program.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is basename in PHP?

Basename in PHP is an inbuilt function in the package and can be used by the developers or programmers to print the name of the file on the output screen. The name can be anything mentioned in the input path and the basename function will search for the filename and print on the screen.

Syntax:

basename (path, suffix)

In the above syntax, the basename function is passed with two parameters. The first is the pathname that is the input to the basename function and the second is the suffix which is an optional one which is used when we need to put more conditions to the function.

  • Path: It is the main parameter passed to the basename function which checks the name of the file and prints the details on the output screen to the user.
  • Suffix: It is an optional parameter that is used when we need to cut off the extensions and want to print only the filename without extension then we can use this parameter. If the filename has any kind of extension like .php, .py, .html, .java, etc. then the extensions will be cut off and only the name of the file will be printed on the output screen.

Examples of basename in PHP

Given below are the examples of basename in PHP:

Example #1

Code:

<?php
$basenamepath = "/dev/leela/login.php";
echo basename($basenamepath) ."<br/>";
?>

Output:

basename in php 1

In the above code, the basename path is the variable that is storing the full path and the echo statement will print the output on the output screen. The input to the basename function is the variable basename path and the output printed will be the filename mentioned in the input. The basename checks the path and cuts off the remaining part of the path and prints the filename itself unless and until any extra arguments are passed to modify the output.

Example #2

Code:

<?php
$basenamepath = "/dev/leela/login.php";
echo basename($basenamepath,".php");
?>

Output:

basename in php 2JPG

In the above example, the basename path is the variable that is storing the full path and the echo statement will print the output in the screen. The input to the basename function is the basename variable and the output printed in the console is slightly different from the above one. Here, the echo statement is having a constraint which states that the filename should be printed without extension i.e. instead of login.php just print login. We can specify some of the extra arguments to the function which will process the data into the mentioned condition and can be coded by the developer as per application functionality.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,694 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Example #3

Code:

<?php
$basepath = "/dev/leela/welcome.php";
echo "The name of the file is ";
echo basename($basepath);
?>

Output:

welcome file

In the above example, the basepath is the name of the variable which is holding the path information and the basename function is used to print the name of the file and the echo statement above the basename is used to print the statement coded by the developer or programmer for the user to understand it easily. If the user is not able to understand what is the functionality of the basename the developer can code it in such a way that the user can understand it easily by adding a few print statements.

Though the basename function has a specific functionality it has few exceptions which need to be understood.

  • The basename function recognizes only a single dot (.) and not double dots (..). In Linux, the double dots are used to move the current directory to the one previous to it. So it doesn’t recognize double dots (..) which is used in Linux and only recognizes a single dot just like the extension for the file.
  • The basename function operates on the input that is passed by the user. It never checks the file system that the user is currently working on. It can be Windows, Linux, Mac, etc. Each and every system have their own format and the function cannot recognize the file system and can give us the wrong output. So it is better to pass the parameter to the basename function.
  • In Linux, the slashes are used to separate directories in a path and in Windows also the slashes are used to separate the folders from one another. In both cases, slashes are used but in a different way. In Windows, the forward slash is used (/) and in some of the environments backslashes (\) are used. So it will be very difficult for the function to understand the environment and act upon it.

Conclusion

In this article, we saw the basename function in PHP and how it works. We also understood the exceptions that we need to take care of when using this function and use it appropriately. While using the basename function we need to pass the parameters properly else we might get wrong output and should take off the pattern i.e. forward slashes and backward slashes.

Recommended Articles

This has been a guide to basename in PHP. Here we discuss the basic concept with different examples of basename in PHP. You may also have a look at the following articles to learn more –

  1. Upload a File in PHP
  2. PHP Database Connection
  3. Login Page in PHP
  4. PHP Superglobal Variables

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More