Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 226
ChrisJJ Avatar asked Sep 23 '15 23:09

ChrisJJ


2 Answers

Upgrade to PHP 7.

Changelog

Version   Description  
7.0.0     Added the optional levels parameter.  
like image 59
Ignacio Vazquez-Abrams Avatar answered Nov 26 '22 11:11

Ignacio Vazquez-Abrams


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);
like image 28
Tarik Avatar answered Nov 26 '22 11:11

Tarik