我试图理解为什么变量函数的PHP文档说明:
变量函数不适用于语言结构,如echo,print,unset(),isset(),empty(),include,require等。 利用包装器函数将这些结构中的任何一个用作可变函数。
我尝试了其中一些并且它们工作得很好:
function animal() { return 'Monkey'; } $animal = 'animal'; echo $animal();返回Monkey - 正如人们所期望的那样。
与print构造相同的结果 - 然后我尝试了unset() ,它也可以正常工作:
function getIndex() { return 0; } $index = 'getIndex'; $array = array( 'Monkey', 'Gorilla' ); unset($array[$index()]); print_r($array);这将返回Array ( [1] => Gorilla ) 。
这里有什么我想念的吗? 只是添加 - 我使用的是PHP 5.5.14。
I'm trying to understand why PHP documentation for variable functions states that:
Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), include, require and the like. 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.
Same result with print construct - then I tried unset() and it also works absolutely fine:
function getIndex() { return 0; } $index = 'getIndex'; $array = array( 'Monkey', 'Gorilla' ); unset($array[$index()]); print_r($array);this returns Array ( [1] => Gorilla ).
Is there something I'm missing here? Just to add - I'm using PHP 5.5.14.
最满意答案
您实际上并没有将任何语言结构用作变量函数:
但试试吧
$function = 'echo'; $function('Hello World');它完全不像文档中所描述的那样不起作用
如手册中所述,使用echo周围的包装函数,然后可以将该函数用作变量函数
function myecho($value) { echo $value; } $function = 'myecho'; $function('Hello World');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');更多推荐
发布评论