Home  >  Article  >  Backend Development  >  PHP function introduction—basename(): returns the file name part of the path

PHP function introduction—basename(): returns the file name part of the path

王林
王林Original
2023-07-27 20:06:003100browse

PHP function introduction—basename(): Returns the file name part of the path

In PHP programming, it is often necessary to operate on file paths. The basename() function can help us quickly and easily obtain the file name part of the path. This article will introduce the function and usage of the basename() function in detail, and demonstrate its practical application through code examples.

The basic syntax of the basename() function is as follows:

string basename ( string $path [, string $suffix ] )

Parameter description:

  • $path: required, indicating the file path, which can be a relative path or an absolute path.
  • $suffix: Optional, indicating the file extension that needs to be removed.

Function function:

  • Get the file name part of the path.

The following uses several examples to demonstrate the use of the basename() function.

Example 1:

$path = "/var/www/html/index.php";
$filename = basename($path);
echo $filename;

Output result:

index.php

In the above example, we pass the file path "/var/www/html/index.php" to basename () function, assign the returned file name to the $filename variable, and finally output the result through the echo statement. As you can see, we successfully obtained the file name part "index.php".

Example 2:

$path = "images/pic.jpg";
$filename = basename($path);
echo $filename;

Output result:

pic.jpg

In this example, we pass the relative path "images/pic.jpg" to the basename() function, similarly Successfully returned only the filename part "pic.jpg".

Example 3:

$path = "/var/www/html/index.php";
$filename = basename($path, ".php");
echo $filename;

Output result:

index

In this example, in addition to passing the file path, we also specify an additional optional parameter ".php" as a suffix. In this way, the basename() function will remove this suffix from the file name and eventually return "index".

The return value of the basename() function is a string that only contains the file name part of the path. If the filename does not exist in the path, the function returns "."

It should be noted that the result of the basename() function may be affected by the operating system. In Windows operating systems, the path separator is "\", while in Linux and macOS operating systems it is "/", so you need to pay special attention when using the basename() function.

Summary:

In PHP programming, the basename() function is very practical and can easily get the file name part from the file path. The basename() function can come in handy in scenarios involving file operations, web links, file uploads, etc. Mastering and flexibly using the basename() function can improve the efficiency of PHP program development and improve the readability of the code.

I believe that through the introduction and examples of this article, readers will have a deeper understanding of the basename() function, and I hope it can provide some help for your PHP programming.

The above is the detailed content of PHP function introduction—basename(): returns the file name part of the path. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn