10

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?

2
  • 4
    Second parameter will be added in PHP 7 (Read the changelog: php.net/manual/en/…) and PHP 5.6 !== PHP 7
    – Rizier123
    Sep 23, 2015 at 23:30
  • You can safely drop 1 as a second param; it will be ignored on PHP 5.x, and it will be filled by default with 1 anyway on PHP 7.
    – raina77ow
    Sep 23, 2015 at 23:34

2 Answers 2

17

Upgrade to PHP 7.

Changelog

Version   Description  
7.0.0     Added the optional levels parameter.  
7
  • Well if he uses the code in production, I wouldn't recommend to upgrade to PHP 7.
    – Rizier123
    Sep 23, 2015 at 23:54
  • I wouldn't recommend upgrading either, but it will get the warning to disappear. Sep 23, 2015 at 23:55
  • Thanks. Extremely poor docs here, showing the V7 version for V5.
    – ChrisJJ
    Sep 25, 2015 at 1:54
  • @ChrisJJ not poor, just trying to be as up to date as possible (we don't have separate docs for each version)… that's what the changelog is for then ;-)
    – bwoebi
    Sep 27, 2015 at 9:29
  • @bwoebi "not poor, just trying to be as up to date as possible" What's poor is that they are not as accurate as they could be. Up-to-date is no excuse for being inaccurate for previous versions.
    – ChrisJJ
    Jul 6, 2017 at 16:52
7

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);
1
  • Not Forgotten $this->dirname_r(); Jul 3, 2018 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.