搞了一下午什么回调函数,匿名函数的,都搞晕了,求方法
再学习PHP,学到了函数部分,选择有点难理解,有木有什么好的学习方法。
新手看谁的视频学习呢
回复内容:
再学习PHP,学到了函数部分,选择有点难理解,有木有什么好的学习方法。
新手看谁的视频学习呢
如果你需要的是视频教程的话,就用 PHP100
吧,比较适合新手的。
地址:http://www.php100.com/html/shipinjiaocheng/PHP100shipinjiaocheng/6.html
视频是需要下载观看的,我貌似还有一份 PHP100
的教程目录,等会传到网盘给你。:)
教程目录传好了,网盘地址:http://pan.baidu.com/s/1gd87Ti7
你可以根据目录选择你需要的内容下载,可能会方便不少。
PHP不懂,打个比喻简单说下匿名函数,回调函数的理解吧。
你去学校拿北大录取通知书,家里准备张罗着宴请亲朋好友,你跟你老爸约定好在你到家前五分钟时给他一条通知短信,告诉你爸收到短信后就点鞭炮,并且拿着大红花在门口等你,你随手拉了身边一个人塞给你爸说‘你让他去点鞭炮,你在门口等我’。在整个过程中:
你 是主函数,你老爸是你的回调函数,因为他在等着你的指示去做事情,你给他注册了一个事件,这个事件就是收到通知短信,他收到这个事件后就让你塞给他的那娃去点鞭炮,这个你连名字都不关注的点炮哥就是匿名函数,你只想让他能干完点鞭炮这事儿就行,以后也不会再让他做其他事情了,所以你也没必要花心思记住他。
额,简单的一个类比吧。
匿名函数粗略地说就是没有名字的函数(PHP 里的有些特殊,用到外层的变量要 use
声明。)
回调就是 callback,就是说 call 回去。就是有一个函数 f ,它调用 (call)另一个函数 g 去做一些事情,然后 g call back f,比如告诉它事情做好了或者搞砸了。你可以简单地理解为 f 委托 g 做一些事,然后 g 向 f 回报结果。
你不管这些概念就已经可以写出很多 PHP 程序了。建议初学的时候对一些概念可以先稍微了解跳过,以后需要用的时候再研究(到时看、写的代码多了,又有实际使用场景,会比较好理解
回调函数就是 —— 告诉你在发生某个事件
之后做某个动作
,前者是回调函数的触发者,后者是回调函数的内容。
匿名函数就是 —— 现在
就需要执行某些动作,而且将来我不必记住这些动作是如何开始执行的(函数名调用)
可以参考javascript嘛
回调函数,可以理解成函数指针或一个能正确指向回调函数的一个值,
因为这个回调函数的方法名称是什么没有任何意义(只是签名作用),它只需要传入正确签名即可,这样一来,完全就可以匿名,此称为匿名函数。
建议回调函数参考 nodejs,匿名函数参考python帮助理解。
回调函数
nodejs有本 nodejs入门经典,可以看看,其中一个例子
<code>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'); }); </code>
运行结果
<code>foo.js read hello.js read Got a response from shapeshed.com AGot a response from www.bbc.co.uk 或 foo.js read hello.js read AGot a response from www.bbc.co.uk Got a response from shapeshed.com </code>
可以理解io输出的是回调函数的内容,他会等到你原本的文件处理或http请求结束之后的反馈函数,所以输出顺序可能每次不是固定,结构前面几位朋友讲的应该可以帮助理解。
匿名函数
可以参考python lambda
lambda x: x * x 相当于一个求 x 平方的函数,但是它没有显式的定义函数,但是可以作为一个函数使用。
例如
<code>In [1]: (lambda x:x *x)(3) Out[1]: 9 </code>
也可结合map生成一个列表
<code>In [2]: map(lambda x: x + 1, [1, 2, 3]) Out[2]: [2, 3, 4] </code>
Ps:对php不太了解,语言应该是相通的吧,希望能帮助你理解,说的不对或和php不同的地方,也请大家指正。
匿名函数就是把函数当成一个对象,你用一个数字,创建一个数组的时候不需要给其名字吧?那么函数也不需要。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
