Home >Backend Development >PHP Tutorial >PHP function introduction—basename(): returns the file name part of the path
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:
Function function:
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!