|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68445 Optional arguments may precede required arguments in function declarations
Submitted: 2014-11-18 19:47 UTC Modified: 2020-06-25 08:12 UTC
From: ajf at ajf dot me Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.6.3 OS: OS X Yosemite
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-11-18 19:47 UTC] ajf at ajf dot me
Optional arguments can be specified before required arguments, which would suggest that you can omit them. Yet, if you actually call the function, it will complain that you didn't pass the required arguments.

The fix would be either to support optional arguments before required arguments at call-time, or to error when this is done in a function declaration.

Test script:

function foo($a = NULL, $b) {


Expected result:

Actual result:
Warning: Missing argument 2 for foo(), called in %s on line 7 and defined in /in/b1HC3 on line 3

Notice: Undefined variable: b in %s on line 4


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-18 20:08 UTC]
Mind that this is also the only way to declare an argument with a nullable class hint.
 [2014-11-18 21:31 UTC] ajf at ajf dot me
That's a fair point. This would change if nullable typehints (as proposed by Levi's RFC: were added.
 [2016-07-14 11:08 UTC]
-Assigned To: +Assigned To: nikic
 [2016-07-14 11:08 UTC]
I think, declaration of arguments without default values after the ones with default values - should be prohibited by compiler (at least since PHP-7.1)
 [2016-07-15 20:07 UTC]
@dmitry: In general I agree, but I don't think we can change this for PHP 7.1. I think many people use this hack to get nullable argument types in PHP < 7.1. They won't be able to use real nullable types unless they require PHP 7.1, which is not realistic.
 [2017-01-08 11:03 UTC]
-Status: Assigned +Status: Open -Assigned To: nikic +Assigned To:
 [2017-01-08 15:37 UTC]
We could perhaps deprecate it, but I doubt there's much advantage to doing so. It would hit a lot of existing code for what is really mostly a cosmetic difference.

Besides, PHP doesn't need to warn of this specific issue, code sniffers and IDEs can do that if they want.
 [2020-06-25 08:12 UTC]
This has been deprecated in PHP 8, with the exception of "Type $x = null" style parameters.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 15 12:01:29 2024 UTC