Why does it only first example not work? What does it happen with the function_exists if it under array_walk? Is it working compiler so, php stack, scope?
I didn't found any explain about it. Or may be I found but didn't understand.
- It doesn't work and throw exception: Warning: array_walk() expects parameter 2 to be a valid callback, function 'test_print' not found or invalid function name in /var/www/sandbox.php on line 4
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
array_walk($fruits, 'test_print');
if(!function_exists('test_print')) {
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
}
- It works
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
if(!function_exists('test_print')) {
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
}
array_walk($fruits, 'test_print');
- It works
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
array_walk($fruits, 'test_print');
- It works
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
array_walk($fruits, 'test_print');
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
php sandbox if you need