Change language

PHP basename () function

|
Syntax:
string basename ($path, $suffix)
Parameters: The basename() function in PHP takes two parameters: a path and a suffix.
  • $path : This parameter is of type string and is required. It specifies the path to the file.
  • $suffix : This is an optional parameter that hides the file extension if it ends with a suffix.
  • Return value:This function returns the base name of the file, the path of which was specified by the user as a parameter.Errors and exceptions :
  • The basename() function does not recognize path components such as "..".
  • The basename() function operates on a user-supplied input string and does not know the real filesystem.
  • Both the forward slash, forward slash (/) and backslash (/) are used as a directory separator character on the Windows platform, while the forward slash (/) is used in other environments.
  • Examples :
    Input: $path = "user01 / home / documents / engineerforengineer.php", Output: engineerforengineer.php Input: $path = "user01 / home / documents / engineerforengineer.php ", $suffix =" .php "Output: engineerforengineer
    The programs below illustrate the basename() function:Program 1 : $path = "user01 / home / documents / engineerforengineer.php" ;  
    // basename() function to display
    // filename with extension echo basename ( $path );  
    ?>
    Output:
    engineerforengineer.php
    Program 2 : $path = "user01 / home / documents / engineerforengineer.php" ;  
    // basename() function to display
    // filename, hiding the extension echo basename ( $path , ". php " );  
    ?>
    Output:
    engineerforengineer
    Link:
    http://php.net/manual/ en / function.basename.php