开发者

A PHP variable with parentheses?

开发者 https://www.devze.com 2023-04-10 02:08 出处:网络
I don\'t understand this code: $outputFunction($dst, $resized, $quality); It\'s not a 开发者_如何学编程function e.g myfunction()

I don't understand this code:

$outputFunction($dst, $resized, $quality);

It's not a 开发者_如何学编程function e.g myfunction()

It's not a variable e.g $variable = $variable2

What is it?

The code works in the script i have downloaded, i just can't figure out how that piece of code can work...maybe i'm just tired or something..

Thanks.


$outputFunction holds the name of the function. Thus, if $outputFunction holds the value "calculate", then calculate($dst, $resized, $quality) is invoked.


To add to sbrattla's answer, you can also define anonymous functions in PHP 5.3 (I think), so

$var = function($a) { /* do something */ return $b; }
echo $var(123);


These are variable functions.

$outputFunction is evaluated to obtain the name of the function to which the operands will be applied.

There's an entire page dedicated to this topic in the PHP manual.


in php you can do something like

$outputFunction = 'myFunction';
$outputFunction(args);

and it works calling the function normally

variable functions


The string should initialized some lines before. You can consider this as a pointer of funcrion which allows to change the executed method.

Php will recognize your syntax and will launch the function named in your string (computed one if you want)

0

精彩评论

暂无评论...
验证码 换一张
取 消