php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39808 null does not set default in function
Submitted: 2006-12-12 20:52 UTC Modified: 2006-12-12 21:59 UTC
From: david at 1st-affiliation dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.0 OS: fedora core 4
Private report: No CVE-ID: None
 [2006-12-12 20:52 UTC] david at 1st-affiliation dot com
Description:
------------
Hi

Since I upgrade the php version, the comportement of the function default argument as change.

Previously, when I set a parameter to "null" the function use the default value of the parameter. 

Now it kept the null value, and I do not find a another way to set the default value.

See the code for more information

Reproduce code:
---------------
<?php
function eat ($what='apple') {
       print 'I eat '.$what;
}

eat(null);

?>

Expected result:
----------------
I eat apple

Actual result:
--------------
I eat 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-12 21:00 UTC] tony2001@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2006-12-12 21:26 UTC] david at 1st-affiliation dot com
tx for your reply

I found may be a answer in the commentary in this page : http://www.php.net/manual/en/functions.arguments.php

But what is not clear is that php behave not like that since the 5.0 version at least.

So why changing an intelligent behavior by something less efficiant ?

This change will oblige to add conciderable coding and make the code more heavy.

We have often functions where not all parameters are need. When there is only one parameter it's simple, but more often there is several optionals arguments, so the question is :

How to set arguments 4, and not 2 and 3 ? Because at the moment it's not impossible to call a function without an argument (ie : diner($starter,,$desert); is wrong, and diner($starter,null, $desert); does not work fine)

So I invite the whole team to look after this behavior to find a better way to use it.

Regards to all 

David
 [2006-12-12 21:41 UTC] tony2001@php.net
/tmp/6.php is
<?php
function eat ($what='apple') {
    var_dump('I eat '.$what);
}

eat(null);
?>

php-src_5_2> ./sapi/cli/php /tmp/6.php
string(6) "I eat "

php-src_5_0> ./sapi/cli/php /tmp/6.php
string(6) "I eat "

php-src_4_4> ./sapi/cli/php /tmp/6.php
string(6) "I eat "

php-src_4_3> ./sapi/cli/php /tmp/6.php
string(6) "I eat "

So this behaviour was introduced BEFORE 4.3.
What are you talking about?
 [2006-12-12 21:54 UTC] david at 1st-affiliation dot com
Oups

Sorry I appologize I did wrong in my code. 

7 years of php use, 1st message to the bug report, and I should shut my mouth, sorry.

Regards

David
 [2006-12-12 21:59 UTC] tony2001@php.net
It's ok ;)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC