php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43543 Misleading TRUE return value for array_walk() and array_walk_recursive().
Submitted: 2007-12-09 14:26 UTC Modified: 2007-12-10 09:45 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: dharma dot yp at in dot ibm dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.2CVS-2007-12-09 (snap) OS: Windows, Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-12-09 14:26 UTC] dharma dot yp at in dot ibm dot com
Description:
------------
In array_walk() and array_walk_recursive() functions, 
1) In PHP 5.2 , if we pass non-existent function( not defined), it is expected to output the warning message with return value "false".It is able output the warning message but not the correct return value. The warning message and return value is as follows. 

"Warning: array_walk(): Unable to call wrong_function() - function does not exist in %s on line %d
bool(true)".
   Where as in PHP6, it works correct and warning message is as follows.

"Warning: array_walk_recursive() : Expects parameter 2 to be valid callback, string given in %s on line %d
NULL".


2) In PHP5.2, if we pass less number of parameters for "callback" function through the array_walk() call, it is expected to ouput the warning message with return value of "false". It is able to output the warning message but not the correct return value.The warning message and return value is as follows.

"Warning: Missing argument 3 for actual_function() in %s on line %d
bool(true)".

This also fails with php 5.3 and php 6.


The return value of " bool(true)" in (1) and (2) will misguide/confuse the developers as the documentation says array_walk()and array_walk_recursive()function calls return TRUE on Success and FALSE on Failure.





Reproduce code:
---------------
/****  for array_walk() ****/
<?php

function actual_function($value, $key, $user_data)
{
  echo $value + $key;
}

$input = array( 0 => 1, 1 => 2, 2 => 3);

var_dump( array_walk($input, "wrong_function"));
var_dump( array_walk($input, "actual_function"));

?>

/**** for array_walk_recursive() ****/
function actual_function($value, $key, $user_data)
{
  echo $value + $key;
}

$input = array( 0 => 1, 1 => 2, 2 => 3);

var_dump( array_walk_recursive($input, "wrong_function"));
var_dump( array_walk_recursive($input, "actual_function"));

?>

Expected result:
----------------
/**** for array_walk() ****/
Warning: array_walk(): Unable to call wrong_function() - function does not exist in %s on line %d
bool(false)

Warning: Missing argument 3 for actual_function() in %s on line %d
bool(false)


/**** for array_walk_recursive() ****/
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
bool(false)


Actual result:
--------------
/**** for array_walk() ****/
Warning: array_walk(): Unable to call wrong_function() - function does not exist in %s on line %d
bool(true)

Warning: Missing argument 3 for actual_function() in %s on line %d
bool(true)


/**** for array_walk_recursive() ****/
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
bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-10 09:45 UTC] jani@php.net
It's fine as it is for 5.2. If it's not ok for 5.3, reopen.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC