php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43558 array_walk() & array_walk_recursive() return value not as documented.
Submitted: 2007-12-11 05:01 UTC Modified: 2007-12-12 17:53 UTC
From: dharma dot yp at in dot ibm dot com Assigned: colder (profile)
Status: Not a bug Package: Arrays related
PHP Version: 6CVS-2007-12-11 (snap) OS: Windows, Linux
Private report: No CVE-ID: None
 [2007-12-11 05:01 UTC] 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)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-11 05:10 UTC] dharma dot yp at in dot ibm dot com
This problem is also there with PHP5.3
 [2007-12-12 17:53 UTC] colder@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

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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Aug 10 16:01:23 2020 UTC