Preg_replace_callback function ($match) use ($ten), what is the meaning of use inside

Source: Internet
Author: User
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;
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.