Preg_replace_callback function ($match) use ($ten), what does the use of the inside mean
$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
);
------to solve the idea----------------------
Use Chinese interpretation
function ($match) use ($ten) {return ($match [0] + $ten);}
Let the variable $ten be used in anonymous functions
Equivalent
$ten = 10;
function foo ($match) {
Global $ten;
Return (($match [0] + $ten));
}
But if $ten's not a global variable, it's going to be a problem.
------to solve the idea----------------------
New closure syntax for PHP 5.3
Closure functions (anonymous functions) can inherit variables from the parent scope any such variables should be passed in using the use language structure
------to solve the idea----------------------
5.2, you can do this.
$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;