The list of function parameters created by the built-in function create_function in php and the function body quotation marks

Source: Internet
Author: User
The list of function parameters created by the built-in function create_function in php and the function body quotation marks

Use the built-in function create_function of php to create a function.
Create_function ('$ item', 'return $ item * 3 ;')
Why do you need to enclose the list of function parameters and function bodies with quotation marks ??


Reply to discussion (solution)

String create_function (string $ args, string $ code)
Both parameters are strings

Can the parameter submitted to the function be a variable?

Because create_function requires two string parameters

Your code is equivalent

function tmp($item) { return $item*3; }$b = array_map('tmp', $a);

Use a closure.
$b = array_map(function($item) { return $item*3; }, $a);

Can the parameter submitted to the function be a variable?

$ Item is a variable submitted to the anonymous function.

$ A = array (100,200,300 );
$ B = array_map (create_function ('$ item', 'return $ item * 3;'), $ );
You mean ('$ item', 'return $ item * 3;') they are only two parameters of the create_function function. Will it automatically create this function internally?

$ A = array (100,200,300 );
$ B = array_map (create_function ('$ item', 'return $ item * 3;'), $ );
Var_dump ($ B );
Create_function requires two strings to be passed in. However, the $ a parameter here is 100 200 300. it is numeric.

Function tmp ($ item) {return $ item * 3 ;}
$ B = array_map ('tmp ', $ );
Do you have any questions?

Create_function requires two strings to be input.
It does not mean that the functions generated by create_function also require two strings to be input.
Create_function requires two strings to be passed in: the parameter list of the anonymous function and the function body.

$ A = array (100,200,300 );
$ B = array_map (create_function ('$ item', 'return $ item * 3;'), $ );
Var_dump ($ B );
Create_function requires two strings to be passed in. However, the $ a parameter here is 100 200 300. it is numeric.

You seem to confuse a concept:
Create_function is an anonymous function. create_function is a function and the input parameter is a string.
The parameters of the created function are the parameters you want to pass, that is, the form parameters of the function. 100,200,300 is the real parameters passed to the created function, since the created anonymous function directly uses this parameter internally, it is passed in as a numeric type and used as a numeric type. If it is not a numeric value, for example, a string of 0123, due to the * operator, it is first converted to a numeric value and then operated:
See:
$ A = array (100,200, "011 ");
$ B = array_map (create_function ('$ item', 'return $ item * 3;'), $ );
Var_dump ($ B );

Thank you for understanding. By the way, there is another question. I am a beginner. do many of the parameters of local functions are defined by variables?

Yes, they are all defined in the form of variables.

Thank you very much for the experts above.

Silently add to favorites

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.