首页 > 解决方案 > PHP 7.3 -> function_exists:神秘的“function_exists”工作

问题描述

为什么它只是第一个例子不起作用?如果 function_exists 在 array_walk 下,它会发生什么?它是这样工作的编译器,php堆栈,范围吗?

我没有找到任何解释。或者可能是我发现但不明白。

  1. 它不起作用并抛出异常:警告:array_walk() 期望参数 2 是有效的回调,未找到函数“test_print”或第 4 行 /var/www/sandbox.php 中的函数名称无效
<?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";
    }
}
  1. 有用
<?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');
  1. 有用
<?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');
  1. 有用
<?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 沙箱

标签: phpphp-7

解决方案


推荐阅读