ChrisJJ
ChrisJJ

Reputation: 2302

How to avoid this bogus "dirname() expects exactly 1 parameter" warning?

On PHP V5.6.13,

dirname("",1);

gives

Warning: dirname() expects exactly 1 parameter, 2 given

despite http://php.net/manual/en/function.dirname.php

string dirname ( string $path [, int $levels = 1 ] )

How can I avoid this bogus warning appearing?

Upvotes: 11

Views: 6713

Answers (2)

Tarik
Tarik

Reputation: 4546

If you don't have PHP 7, use the function below to have a recursive dirname with levels:

function dirname_r($path, $count=1){
    if ($count > 1){
       return dirname(dirname_r($path, --$count));
    }else{
       return dirname($path);
    }
}
echo dirname_r(__FILE__, 2);

Upvotes: 7

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799082

Upgrade to PHP 7.

Changelog

Version   Description  
7.0.0     Added the optional levels parameter.  

Upvotes: 18

Related Questions