如何在不使用 create_function 的情况下重写示例? [英] How to rewrite an example without the use of create_function?

查看:25
本文介绍了如何在不使用 create_function 的情况下重写示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当查看 PHP 的 create_function 时,它说:

When looking at PHP's create_function it says:

如果您使用的是 PHP 5.3.0 或更高版本的原生匿名函数应该改用.

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

我想重新创建与 create_function 相同的功能,但使用 匿名函数.我不知道如何,或者我是否正确地接近它.

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.

本质上,我如何更改以下内容,以便不再使用 create_function 但仍然可以输入我想用我自己的参数进行评估的自由格式公式?

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";

示例取自 PHP 的 create_function 页面.

Example taken from PHP's create_function page.

注意:

看起来在上面的例子中我可以传入一个任意字符串,并为我编译它.我可以在不使用 create_function 的情况下以某种方式做到这一点吗?

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?

推荐答案

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

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

这篇关于如何在不使用 create_function 的情况下重写示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆