PHP:如何使变量在create_function()中可见?

PHP:如何使变量在create_function()中可见?,php,preg-replace,global-variables,preg-replace-callback,create-function,Php,Preg Replace,Global Variables,Preg Replace Callback,Create Function,此代码: $t = 100; $str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/", create_function( '$matches', 'return $matches[1] + $t;' ),

此代码:

$t = 100;
$str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/",
                            create_function(
                                  '$matches',
                                  'return $matches[1] + $t;'
                            ), $func);
如何在preg_replace()函数中的create_function()中使$t可见?

在使用
use
语法时,An可以工作:

$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);
在使用
use
语法的同时,可以使用:

$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);
和让任何函数看到全局变量的方式相同

$str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/",
                            create_function(
                                  '$matches',
                                  'global $t; return $matches[1] + $t;'
                            ), $func);
和让任何函数看到全局变量的方式相同

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

您无法使变量可访问,但在您的情况下,您可以使用以下值:

$t = 100;
$str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/",
                            create_function(
                                  '$matches',
                                  'return $matches[1] + ' . $t .';'
                            ), $func);
但是,强烈建议您在此处使用
函数($matches)use($t){}
语法()

还有一个eval修饰符用于
preg\u replace

$str = preg_replace("/(Name[A-Z]+[0-9]*)/e", '$1+'.$t, $func);

但我感觉您的函数在这里使用了错误的运算符,或者使用了错误的模式/子模式。

您无法访问变量,但在您的情况下,您可以使用以下值:

$t = 100;
$str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/",
                            create_function(
                                  '$matches',
                                  'return $matches[1] + ' . $t .';'
                            ), $func);
但是,强烈建议您在此处使用
函数($matches)use($t){}
语法()

还有一个eval修饰符用于
preg\u replace

$str = preg_replace("/(Name[A-Z]+[0-9]*)/e", '$1+'.$t, $func);

但是我感觉你的函数在这里使用了错误的操作符,或者是错误的模式/子模式。

你可以使用
$GLOBALS
,但这并不推荐

$str = preg_replace_callback ( "/(Name[A-Z]+[0-9]*)/", create_function ( '$matches', 'return $matches[1] + $GLOBALS["t"];' ), $func );
更好的解决方案


匿名函数。。如果您不喜欢使用它,您也可以在以数组格式获得结果后执行
array\u walk
(),然后将
$t
作为适当的函数参数传递

您可以使用
$GLOBALS
,但建议不要太高

$str = preg_replace_callback ( "/(Name[A-Z]+[0-9]*)/", create_function ( '$matches', 'return $matches[1] + $GLOBALS["t"];' ), $func );
更好的解决方案


匿名函数。。如果您不喜欢使用它,您也可以在以数组格式获得结果后执行
array\u walk
(),然后将
$t
作为适当的函数参数传递给anonymous,只需使用关键字useglobal 在创建函数中使用全局

function()使用($var1,$var2…等){code here}

创建_func($args,'global$var1,$var2;在此处编码;')


在匿名中,只需使用关键字使用全局 在创建函数中使用全局

function()使用($var1,$var2…等){code here}

创建_func($args,'global$var1,$var2;在此处编码;')


您应该使用闭包而不是函数($matchtes)使用($t){/*..*/}@hakre
“1”+100
;)但是我明白了,您的意思是:正则表达式匹配以
名称开始的内容,因此函数将始终返回
100
=$t
)。可能不需要。解决方案取决于您使用的PHP版本。理想情况下,解决方案是使用闭包将$t变量传递给
use
构造。这至少需要PHP5.3。顺便说一下,如果$t的值没有任何变化,你可以考虑使用一个常量,它可以在任何上下文中使用。你应该使用闭包而不是代码>函数($MatCHTES)使用($T){/**/} <代码> @ HakRE代码>“1”+100 < /C>;但是我明白了,您的意思是:正则表达式匹配以
名称开始的内容,因此函数将始终返回
100
=$t
)。可能不需要。解决方案取决于您使用的PHP版本。理想情况下,解决方案是使用闭包将$t变量传递给
use
构造。这至少需要PHP5.3。顺便说一下,如果$T的值没有任何变化,你可以考虑使用一个常数,它可以在任何上下文中使用。code>global
只从全局范围导入变量,而不是从定义函数的范围导入变量,就像
use
那样。小心
global
仅从全局范围导入变量,而不是从定义函数的范围导入变量,就像
use
那样。