php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66031 use() problem with array_walk[_recursive]
Submitted: 2013-11-04 13:57 UTC Modified: 2013-11-04 14:27 UTC
From: bugzilla77 at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.5.6RC1 OS: Win 7 64
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bugzilla77 at gmail dot com
New email:
PHP Version: OS:

 

 [2013-11-04 13:57 UTC] bugzilla77 at gmail dot com
Description:
------------
use() do not work with array_walk[_recursive]

Test script:
---------------
<?php
 $a=['a'=>'p','b'=>['c'=>'a','d'=>'s'],'e'=>'s'];

 $c=0;array_walk($a,function()use($c){$c++;});

 $r='';array_walk_recursive($a,function($v)use($r){$r.=$v;});

 print($c.'/'.$r); // 3/pass
?>


Expected result:
----------------
3/pass

Actual result:
--------------
0/

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-11-04 14:27 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2013-11-04 14:27 UTC] johannes@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

You are passing the bound variable by value, not by reference. Try "use (&$c)" else $c++ will increment on a copy. Same for the other example with $r.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 04:00:01 2026 UTC