php - PHP 7.3 -> function_exists:神秘的“function_exists”工作
问题描述
为什么它只是第一个例子不起作用?如果 function_exists 在 array_walk 下,它会发生什么?它是这样工作的编译器,php堆栈,范围吗?
我没有找到任何解释。或者可能是我发现但不明白。
- 它不起作用并抛出异常:警告: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";
}
}
- 有用
<?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');
- 有用
<?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');
- 有用
<?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 沙箱
解决方案
推荐阅读
- android - Android 应用程序包 -> apk 不包含必需的本机库
- javascript - 我怎样才能调用反应状态不同的js
- java - 如何将属性文件中的所有值作为键传递给 hashmap
- scala - 在 CV 网格中为 Spark xgBoost 模型设置 scalePosWeight 参数
- c# - 使用 if else 获取文件上传中的路径文件名
- python - Scrapy 数据流和项目和项目加载器
- apache-spark - 在 Pyspark/Jupyter 中设置 spark.local.dir
- c++ - C++ Curl 多执行阻塞问题
- reinforcement-learning - 强化学习中策略梯度下降的奖励函数
- python - 如何初始化 gensim LDA 主题模型?