search
HomeBackend DevelopmentPHP Tutorial搞了一下午什么回调函数,匿名函数的,都搞晕了,求方法

再学习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不同的地方,也请大家指正。

匿名函数就是把函数当成一个对象,你用一个数字,创建一个数组的时候不需要给其名字吧?那么函数也不需要。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool