How to rewrite an example without the use of create_function?

安稳与你 提交于 2019-12-02 00:35:46

问题


When looking at PHP's create_function it says:

If you are using PHP 5.3.0 or newer a native anonymous function should be used instead.

I want to recreate same functionality of create_function but using an anonymous function. I do not see how, or if I am approaching it correctly.

In essence, how do I change the following so that I no longer use create_function but still can enter free-form formula that I want to be evaluated with my own parameters?

$newfunc = create_function(
    '$a,$b',
    'return "ln($a) + ln($b) = " . log($a * $b);'
);
echo $newfunc(2, M_E) . "\n";

Example taken from PHP's create_function page.

Note:

It looks like with the above example I can pass in an arbitrary string, and have it be compiled for me. Can I somehow do this without the use of create_function?


回答1:


$newfunc = function($a, $b) {
    return "ln($a) + ln($b) = " . log($a * $b);
}

echo $newfunc(2, M_E) . "\n";


来源:https://stackoverflow.com/questions/39666228/how-to-rewrite-an-example-without-the-use-of-create-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!