PHP built-in function create_function created function argument list and function body quotation mark problem

Source: Internet
Author: User
Functions created by create_function

Use PHP's built-in function to create_function the created function.
Create_function (' $item ', ' return $item; ')
Let me ask you something. Why do you create the function parameter list and function body with quotation marks??


Reply to discussion (solution)

String Create_function (String $args, String $code)
Two parameters are all strings

The arguments that are submitted to the function are not and can be variables.

Because create_function requires the passing of 2 string arguments

Your code is equivalent to

function tmp ($item) {return $item;} $b = Array_map (' tmp ', $a);

You can do it with a closed bag.
$b = Array_map (function ($item) {return $item;}, $a);

The arguments that are submitted to the function are not and can be variables.

$item is already a variable submitted to an anonymous function.

$a =array (100,200,300);
$b =array_map (create_function (' $item ', ' return $item; '), $a);
You mean to say (' $item ', ' return $item; ') The two of them are just the two parameters of the Create_function function, and it will automatically create this function inside.

$a =array (100,200,300);
$b =array_map (create_function (' $item ', ' return $item; '), $a);
Var_dump ($b);
Create_function required to pass in two strings but I have a $ A argument here 100 200 300 is a numeric type Yes help

function tmp ($item) {return $item;}
$b = Array_map (' tmp ', $a);
Do you have any questions about it?

Create_function requires two strings to be passed in
Does not mean that a function produced by Create_function also requires the passing of two strings
Create_function requires two strings to be passed in, namely: The parameter list of the anonymous function and the function body

$a =array (100,200,300);
$b =array_map (create_function (' $item ', ' return $item; '), $a);
Var_dump ($b);
Create_function required to pass in two strings but I have a $ A argument here 100 200 300 is a numeric type Yes help

You seem to confuse a concept:
Create_function is the creation of an anonymous function, which is itself (create_function) is a function that needs to pass in a parameter that is a string.
The parameters of the created function are the parameters you want to pass, that is, the function parameter, 100,200,300 is the argument to the created function, because the anonymous function is created directly using the parameter, so passed in is a numeric type, the use of numerical type. If the pass is not a numeric value, for example, a string of 0123, because of the * operator, it will first be converted to a numeric value and then operation:
Please see:
$a =array (100,200, "011");
$b =array_map (create_function (' $item ', ' return $item; '), $a);
Var_dump ($b);

Thank you for understanding. Yes, there's one more question I'm a beginner. There are a lot of people who don't understand that the parameters of the local function are defined by variables.

Yes, it's all defined in the form of a variable.

Thank you so much for the top guys.

Silent Collection

  • 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.