#43558 [Opn->Bgs]: array_walk() & array_walk_recursive() return value not as documented.
| From: | colder@php.net | Date: | Wed, 12 Dec 2007 17:53:16 +0000 |
| Subject: | #43558 [Opn->Bgs]: array_walk() & array_walk_recursive() return value not as documented. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-120610@lists.php.net to get a copy of this message | ||
ID: 43558
Updated by: colder@php.net
Reported By: dharma dot yp at in dot ibm dot com
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: Windows, Linux
PHP Version: 6CVS-2007-12-11 (snap)
-Assigned To:
+Assigned To: colder
New Comment:
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
This is expected behavior:
1) an internal function call with incorrect parameters will (usually)
issue a warning and return NULL without executing the function.
2) an userland function call missing a parameter will issue a warning
and execute the function with the parameter being undefined.
This is exactly what's happening here.
Previous Comments:
------------------------------------------------------------------------
[2007-12-11 05:10:44] dharma dot yp at in dot ibm dot com
This problem is also there with PHP5.3
------------------------------------------------------------------------
[2007-12-11 05:01:27] dharma dot yp at in dot ibm dot com
Description:
------------
The return value returned by array_walk() and array_walk_recursive()
not as per the documentation. As per the documentation when these
function fail it is expected to return FALSE, but it returns different
values.
Documentation links:
http://in.php.net/manual/en/function.array-walk.php
http://in.php.net/manual/en/function.array-walk-recursive.php
Below are two scenarios where return value differ :
1) When function is called with nonexistent callback function, it
returns NULL (expected value is bool(false) ) as return value along with
warning message(warning message as expected)
2)When there are less number of arguments passed for the callback
function then it return bool(true) along with a warning message.
As we notice the function does get called and the code is executed,
hence should it be considered as complete and return value be bool(true)
? or since there is a warning indicating that there were problem and the
return value be bool(false) ?
It would be nice to have consistent return value & sync the code with
the documentation or documentation with the current code.
Reproduce code:
---------------
<?php
function actual_function($index, $key, $user_data)
{
echo "callback function called\n";
}
$input = array( array(1), array(3));
var_dump( array_walk($input, "non_existent"));
var_dump( array_walk($input, "actual_function"));
var_dump( array_walk_recursive($input, "non_existent"));
var_dump( array_walk_recursive($input, "actual_function"));
?>
Expected result:
----------------
Warning: array_walk() expects parameter 2 to be valid callback, string
given in %s on line %d
bool(false)
Warning: Missing argument 3 for actual_function() in %s on line %d
bool(false)
Warning: array_walk_recursive() expects parameter 2 to be valid
callback, string given in %s on line %d
bool(false)
Warning: Missing argument 3 for actual_function() in %s on line %d
bool(false)
Warning: Missing argument 3 for actual_function() in %s on line %d
bool(false)
Actual result:
--------------
Warning: array_walk() expects parameter 2 to be valid callback, string
given in %s on line %d
NULL
Warning: Missing argument 3 for actual_function() in %s on line %d
callback function called
bool(true)
Warning: array_walk_recursive() expects parameter 2 to be valid
callback, string given in %s on line %d
NULL
Warning: Missing argument 3 for actual_function() in %s on line %d
callback function called
bool(true)
Warning: Missing argument 3 for actual_function() in %s on line %d
callback function called
bool(true)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43558&edit=1