PHP:如何使变量在create_function()中可见? [英] PHP: How to make variable visible in create_function()?

查看:150
本文介绍了PHP:如何使变量在create_function()中可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码:

This code:

$t = 100;
$str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/",
                            create_function(
                                  '$matches',
                                  'return $matches[1] + $t;'
                            ), $func);

如何通过preg_replace()函数中的create_function()使$ t可见?

How to make $t visible from create_function() in preg_replace() function?

推荐答案

匿名功能可以在使用 use 语法的情况下工作:

An anonymous function would work, while making use of the use syntax:

$t = 100;
$str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/",
    function($matches) use($t) // $t will now be visible inside of the function
    {
        return $matches[1] + $t;
    }, $func);

这篇关于PHP:如何使变量在create_function()中可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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