[英]How to put variables into calling scope in PHP?
The built-in PHP function parse_str( $str ) parses a query string and sets the variables from it into the current scope. 内置的PHP函数parse_str($ str)解析查询字符串,并将其中的变量设置为当前作用域。 Is there any way for me to create my own function that does something like this and returns values to the scope of the caller? 有什么办法让我创建自己的函数,该函数执行类似的操作并将值返回给调用者的作用域?
Example: 例:
function checkset()
{
global $inputs;
for( $counter = 0, $varname = func_get_arg($counter), $counter++)
{
if(isset($inputs[$varname]))
{
calling_scope( $varname, $inputs[$varname] );
}
}
return;
}
where calling_scope( $varname, $value) would be a function that sets $$varname in the scope where the function is called to $value. 其中calling_scope($ varname,$ value)是将在将函数调用为$ value的范围内设置$$ varname的函数。 Any help is appreciated. 任何帮助表示赞赏。
You can't inject variables into the calling scope. 您不能将变量注入调用范围。 You however can use extract
to turn the keys/values from an associative array into variables in the local scope. 但是,您可以使用extract
将关联数组中的键/值转换为局部作用域中的变量。 This is probably as close as you're going to get. 这可能与您将要达到的程度差不多。
function checkset() {
return array('name' => 'bob', 'age' => 45);
}
extract(checkset());
echo $name; // bob
echo $age; // 45
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.