【发布时间】:2018-04-17 09:32:38
【问题描述】:
有没有办法将变量传递到 PHP 中类似于 Javascript 的函数范围?到目前为止,这是我的理解:
// Simple Function
$hello_world = function(){
echo 'Hello World 1';
};
$hello_world();
// Using Global Variable
$text = 'Hello World 2';
$hello_world = function(){
// Need to Add Global
global $text;
// Adding Text
echo $text;
};
$hello_world();
// Dynamically Created Functions
$function_list = [];
// Function to Create Dynamic Function
function create_dynamic_function($function_name,$response_text){
global $function_list;
$function_list[$function_name] = function(){
echo $response_text; // Want to Echo the Input $response_text, but it cannot find it
};
}
create_dynamic_function('Hello','Hello World 3');
$function_list['Hello'](); // Doesn't Work
我想将响应文本传递给新生成的函数,而不必检查什么是 response_text。它可以是字符串、整数、布尔值、对象等
【问题讨论】:
-
function() use($response_text)
标签: php