$string = "Some numbers: one: 1; two: 2; three: 3 end";$ten = 10;$newstring = preg_replace_callback( '/(\d+)/', function($match) use ($ten) { return (($match[0] + $ten)); }, $string );echo $newstring;
use 中文释义 使用
function($match) use ($ten) { return (($match[0] + $ten)); }
让变量 $ten 在匿名函数中可以被使用
相当于
$ten = 10;
function foo($match) {
global $ten;
return (($match[0] + $ten));
}
不过如果 $ten 不是全局变量的话就有麻烦了
php 5.3新增的闭包语法
闭包函数(匿名函数)可以从父作用域中继承变量 任何此类变量都应该用 use 语言结构传递进去
use 中文释义 使用
function($match) use ($ten) { return (($match[0] + $ten)); }
让变量 $ten 在匿名函数中可以被使用
相当于
$ten = 10;
function foo($match) {
global $ten;
return (($match[0] + $ten));
}
不过如果 $ten 不是全局变量的话就有麻烦了
php 5.3新增的闭包语法
闭包函数(匿名函数)可以从父作用域中继承变量 任何此类变量都应该用 use 语言结构传递进去
5.2 不能用你可以这样
$string = "Some numbers: one: 1; two: 2; three: 3 end";$ten = 10;$newstring = preg_replace_callback( '/(\d+)/', create_function('$match', "return $match[0] + $ten;"), $string );echo $newstring;
5.2 不能用你可以这样
$string = "Some numbers: one: 1; two: 2; three: 3 end";$ten = 10;$newstring = preg_replace_callback( '/(\d+)/', create_function('$match', "return $match[0] + $ten;"), $string );echo $newstring;
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号