The basename() function in PHP is a very useful function used to obtain the file name part of the specified path. Here is a detailed introduction to the usage of the PHP basename() function.
1. Function prototype
basename (string $path, string $suffix): string
2. Function parameters
$path: file needs to be obtained The path string of the name. Can be a relative path or an absolute path.
$suffix: Optional, if the file name ends with $suffix, this part will be removed.
3. Function return value
The return value of the basename() function is the file name under the specified path. If there is a $suffix parameter, delete the part ending with $suffix in the file name.
4. Function Example
In the following example, we will use the following path:/var/www/html/test.txt
echo basename('/var/www/html/test.txt'); // Output: test.txt
echo basename('/var/www/html/test.txt', '.txt'); // Output: test
echo basename(__FILE__); // Output: basename.php
$dir = dirname('/var /www/html/test.txt');
echo basename($dir); // Output: html
5. Notes
echo basename('/var/www/html/'); // Output: ''
echo basename('.'); // Output: '.'
echo basename('..'); // Output: '..'
echo basename('/var/www/html/test.TXT', '.txt'); // Output: test.TXT
echo basename('/var/www/html/test'); // Output: test
The above is the detailed explanation of the PHP basename() function. Through this function, we can easily obtain the file name or folder name and perform corresponding operations. In the actual development process, we can also use it in combination with other functions and variables as needed to achieve more flexible and efficient results.
The above is the detailed content of Detailed explanation of PHP basename() function usage. For more information, please follow other related articles on the PHP Chinese website!