[英]PHP Variable Functions and language constructs
I'm trying to understand why PHP documentation for variable functions states that: 我试图了解为什么变量函数的PHP文档指出:
Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), include, require and the like.
变量函数不适用于语言构造,例如echo,print,unset(),isset(),empty(),include,require等。 Utilize wrapper functions to make use of any of these constructs as variable functions.
利用包装器函数将这些构造中的任何一个用作变量函数。
I tried some of these and they work just fine: 我尝试了其中的一些,它们工作得很好:
function animal() {
return 'Monkey';
}
$animal = 'animal';
echo $animal();
returns Monkey
- just as one would expect. 返回
Monkey
就像人们期望的那样。
Same result with print
construct - then I tried unset()
and it also works absolutely fine: 与
print
结构相同的结果-然后我尝试了unset()
,它也可以正常工作:
function getIndex() {
return 0;
}
$index = 'getIndex';
$array = array(
'Monkey',
'Gorilla'
);
unset($array[$index()]);
print_r($array);
this returns Array ( [1] => Gorilla )
. 这将返回
Array ( [1] => Gorilla )
。
Is there something I'm missing here? 我在这里想念什么吗? Just to add - I'm using PHP 5.5.14.
只是添加-我正在使用PHP 5.5.14。
They mean another usage: 它们意味着另一种用法:
<?php
$var = "some variable";
$a = "unset"; //also print, isset, echo, include
// you cannot do this:
$a($var);
Of course you can unset or print variable with string containing function name... 当然,您可以使用包含函数名称的字符串来取消设置或打印变量。
(Or am I missing something? :) ) (或者我缺少什么?
You're not actually using any language constructs as a variable function: 您实际上并没有使用任何语言构造作为变量函数:
But try 但是尝试
$function = 'echo';
$function('Hello World');
and it won't work, exactly as described in the docs 完全不符合文档中的描述
Use a wrapper function around echo as described in the manual, and then you can use that function as a variable function 如手册所述,在回声周围使用包装函数,然后可以将该函数用作变量函数
function myecho($value) {
echo $value;
}
$function = 'myecho';
$function('Hello World');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.