php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46083 PHP allows default parameters to appear before required parameters
Submitted: 2008-09-15 17:07 UTC Modified: 2010-12-22 13:57 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: kevin dot benton at beatport dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.6 OS: All
Private report: No CVE-ID: None
 [2008-09-15 17:07 UTC] kevin dot benton at beatport dot com
Description:
------------
PHP allows default parameters to appear before required parameters

From http://www.php.net/manual/en/functions.arguments.php,

It reports that...

<?php
function makeyogurt($type = "acidophilus", $flavour)
{
    return "Making a bowl of $type $flavour.\n";
}
 
echo makeyogurt("raspberry");   // won't work as expected
?>

will generate this output:

Warning: Missing argument 2 in call to makeyogurt() in 
/usr/local/etc/httpd/htdocs/phptest/functest.html on line 41
Making a bowl of raspberry .

I think this is simply nasty behavior.  If $type is defaulted and not specified, PHP should know that it's a default parameter from the parameter count and shift accordingly or generate a syntax error.  A default parameter that appears to the left of a required parameter isn't optional with the way parameters are processed in PHP today.

I would rather have seen one of these results:

Error: makeyogurt() specifies required parameters after defaulted parameters in /usr/local/etc/httpd/htdocs/phptest/functest.html on line 41.

or

Making a bowl of acidophilus raspberry.

While the second solution was rejected in http://bugs.php.net/24153, I believe the first solution is still valid and quite frankly, appropriate.  If nothing else, this should be an option in php.ini to prohibit optional parameters before required parameters.

C programmers are familiar with being able to do this by turning on -Wall -Werror

Reproduce code:
---------------
See description

Expected result:
----------------
See description

Actual result:
--------------
See description

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-22 13:57 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 13:57 UTC] johannes@php.net
This is an side effect of the possibility to allow NULL values with type hinted parameters.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Jun 04 15:00:02 2026 UTC