All callback functions and anonymous functions in the afternoon are lost. How can this problem be solved?

Source: Internet
Author: User
After learning PHP, I learned the function part. It's hard to understand the choice. What are some good learning methods. Who is new to learning from the video? After learning PHP, I learned the function section. It's hard to understand the choice. What are some good learning methods.

Who's going to watch video learning?

Reply content:

After learning PHP, I learned the function part. It's hard to understand the choice. What are some good learning methods.

Who's going to watch video learning?

If you need a video tutorial, usePHP100It is suitable for beginners.
Address: http://www.php100.com/html/shipinjiaocheng/PHP100shipinjiaocheng/6.html
You need to download and watch the video.PHP100Will be uploaded to the online storage. :)
Tutorial directory passed, Network Disk address: http://pan.baidu.com/s/1gd87Ti7
You can select the desired content to download Based on the directory, which may be more convenient.

PHP doesn't understand. Let's give a simple analogy to the anonymous function. Let's take a look at the callback function.
You went to school to get the admission notice from Peking University. Zhang Luo was prepared to entertain friends and family at home. You agreed with your dad to send him a notification text message five minutes before you got home, tell your dad to set off firecrackers after receiving the text message and wait for you at the door with a big red flower. YouWith the help of a person around meYou told your dad, 'You asked him to set off firecrackers, and you waited for me at the door '. Throughout the process:
YouIt's the main function. Your dad is yours.Callback FunctionBecause he is waiting for your instructions to do things, you have registeredEventThis event isReceive notification text messageAfter receiving the eventThe one you gave him.Set off firecrackers.You don't even care about the name.YesAnonymous FunctionsYou just want him to finish the task of setting off firecrackers, and will not let him do other things in the future, so you do not need to remember him.
A simple analogy.

An anonymous function is a function without a name (some special functions in PHP use outer variablesuseStatement .)

Callback is callback, that is, call back. That is, there is a function f, which calls another function g to do something, and then g call back f, such as telling it that things are done or screwed up. You can do something for f to delegate g, and then g returns the result to f.

You can write many PHP programs regardless of these concepts. It is recommended that you skip some concepts when you are a beginner, and then study them when you need to use them (more code will be read and written at that time, and more practical use cases will be available for better understanding.

The callback function is -- tell youAn event occurs.AfterPerform an actionThe former is the trigger of the callback function, and the latter is the content of the callback function.
The anonymous function is --NowYou need to execute some actions, and in the future, I don't have to remember how these actions start to be executed (function name call)

Refer to javascript.

A callback function can be understood as a function pointer or a value that correctly points to the callback function,
Because the method name of this callback function does not have any meaning (only for signature), it only needs to pass in the correct signature, so that it can be completely anonymous. This is called an anonymous function.

We recommend that you refer to nodejs for callback functions and python for anonymous functions.

Callback Function
Nodejs has a classic introduction to node. js. You can refer to one example.

var fs   = require('fs'),    http = require('http');http.get({host:'shapeshed.com'}, function(res){    console.log('Got a response from shapeshed.com');}).on('error', function(e){    console.log('There was an error from shapeshed.com');});fs.readFile('foo.js', 'utf8', function(err, data){    if (err) throw err;    console.log('foo.js read');});http.get({host:'www.bbc.co.uk'}, function(res){    console.log('Got a response from www.bbc.co.uk');}).on('error', function(e){    console.log('There was an err from bbc.co.uk');});fs.readFile('hello.js', 'utf8', function(err, data){    if (err) throw err;    console.log('hello.js read');});

Running result

Foo. js readhello. js readGot a response from shapeshed. comAGot a response from www. bbc. co. uk or foo. js readhello. js readAGot a response from www. bbc. co. ukGot a response from shapeshed.com

I/O outputs the content of the callback function. It waits until your original file processing or the feedback function after the http request ends. Therefore, the output sequence may not be fixed at a time, the previous several friends mentioned the structure should be helpful.

Anonymous Functions
Refer to python lambda
Lambda x: x is equivalent to a function that calculates the square of x, but it does not explicitly define a function, but can be used as a function.

For example

In [1]: (lambda x:x *x)(3)Out[1]: 9

You can also use map to generate a list.

In [2]: map(lambda x: x + 1, [1, 2, 3])Out[2]: [2, 3, 4]

Ps: I don't know much about php. The language should be the same. I hope it can help you understand it. If it is not correct or different from php, please correct me.

An anonymous function treats a function as an object. You do not need to give it a name when creating an Array Using a number? Then the function is not required.

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.