PHP dirname() Function: Description, Parameters, Return Value, and Examples

The article explains PHP's dirname() function, detailing its purpose of returning the directory portion of a path, describing the $path parameter, the return values, and providing clear code examples demonstrating typical usage scenarios.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP dirname() Function: Description, Parameters, Return Value, and Examples

dirname() returns the directory part of a given path.

Explanation: When given a full file path string, the function removes the file name and returns the parent directory. If the path contains no directory separator, it returns a dot ('.') representing the current directory.

Parameter: $path – a string containing a path. Both forward slash (/) and backslash (\) are accepted as directory separators on Windows; on other systems only forward slash is used.

Return value: The parent directory of $path. If there is no slash, returns '.'; otherwise returns the string with the trailing '/' and the last component removed.

Examples:

<?php
echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc
echo "2) " . dirname("/etc/") . PHP_EOL;      // 2) /
echo "3) " . dirname(".") . PHP_EOL;          // 3) .
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendfunctionpathdirname
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.