foreach array function storing in a variable?
$numbers = ar开发者_JS百科ray('1','2');
$numberlist = foreach($numbers as $number) {
echo $number;
}
As you can see what I'm trying to do it doesn't work is there any other way to store a foreach function as a variable?
$numberList = function( $input )
{
foreach( $input as $v )
echo $v;
};
$numberList( $numbers );
See PHP Anon
Note: Anonymous functions are available since PHP 5.3.0.
(The function should be $numberList with a capital L in order for it to work properly.)
If you're trying to store a code reference to the foreach loop in the $numberlist
variable, that can't be done: loops are not functions.
If you want an object you can cycle on, you need to build an interator. If this is the case, I suggest you take a look at Standard PHP Library.
I think what Matthew is trying to do is store a function in a variable in PHP. I think this link is what you're looking for:
http://php.net/manual/en/functions.variable-functions.php
精彩评论