PHP pathinfo() Function

The pathinfo() function returns an array that contains information about a path.If the options parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename.

mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )

pathinfo() returns information about path: either an associative array or a string, depending on options.

The following array elements are returned : -

  • [dirname]
  • [basename]
  • [extension]
  • Example -

    The above example will output : -

    /www/htdocs/inc
    lib.inc.php
    php
    lib.inc
    Example #2 pathinfo() example showing difference between null and no extension
    
    

    The above example will output something similar to : -

    string(0) ""
    
    Notice: Undefined index: extension in test.php on line 6
    NULL

    Example -

    The above example will output something similar to : -

    Array
    (
        [dirname] => /some/path
        [basename] => .test
        [extension] => test
        [filename] => 
    )

    ParameterDescription
    pathThe path to be parsed
    optionsIf present, specifies a specific element to be returned; one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.