php arrow is a short closure, also called arrow function. It is a short function written in PHP. This function is very useful when passing a closure to the function. A short closure can only have one expression, which means there cannot be multiple lines in the closure body. The purpose of a short closure is to reduce redundancy.
The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.
What does arrow mean in php?
PHP 7.4 New syntax of arrow function examples
Short closure, also called arrow function, is a A short function written in PHP. This feature is very useful when passing closures to functions, such as using array_map or array_filter function.
This is what they look like:
// Collection of Post objects
$posts = [/* … */]; $ids = array_map(fn($post) => $post->id, $posts);
Whereas before, you had to write like this:
$ids = array_map(function ($post) { return $post->id; }, $posts);
Let’s summarize how to use short closure functions.
Available in PHP 7.4
It can only contain one expression starting with the fn keyword, that is, the return expression return keyword can Type hinting can be done by ignoring parameters and return types.
A more stringent type definition of the above example can be written as:
$ids = array_map(fn(Post $post): int => $post->id, $posts);
There are two points that need to be mentioned:
It is also allowed to use the extension operator to allow references, and both parameters can be used as return values
If you want to return the result by reference, you should use the following syntax:
fn&($x) => $x
Simple In short, short closures function the same as ordinary closures, except that only one expression is allowed.
Single line
You should understand it correctly: a short closure can only have one expression. This means that there cannot be multiple lines in the closure body.
The reason is as follows: The purpose of short closures is to reduce redundancy. Of course, fn is shorter than function in any case. However, RFC creator Nikita Popov I think if you're dealing with functions that are multi-line expressions, you get even less benefit from using closures.
After all, the definition of multi-line closures is already very redundant, so there will not be much difference between the presence and absence of these two keywords (function and return).
Whether you agree with this point of view is up to you. While I can think of many scenarios for single-line closures in my projects, there are also many scenarios for multi-line closures, and personally I would prefer a shorter syntax for those cases.
There is hope though: multi-line short closures may be added in the future, but that would also be a separate RFC.
Values of external scope
Another notable feature of short closures and ordinary closures is that short closures can access data in external scopes without using the use keyword.
$modifier = 5; array_map(fn($x) => $x * $modifier, $numbers);
It should be noted that variables in the external scope cannot be modified. Because it is pass by value rather than pass by reference. This means that you can change the $modifier variable inside the short closure, but it will not change the variable in the outer scope. The $modifier variable has an impact.
Of course, there is an exception, and that is the $this keyword, which has exactly the same effect as in a normal closure:
array_map(fn($x) => $x * $this->modifier, $numbers);
Development prospects
The multi-line closure I have already mentioned is still a development possibility in the future. The other one is in my mind The idea is to allow the use of short closures in classes, such as getters and setters Function.
class Post { private $title; fn getTitle() => $this->title; }
All in all, short closures are a very welcome feature, although there are many areas that need improvement. The most likely one is multi-line closures.
The above is the detailed content of what does php arrow mean. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

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

SublimeText3 Linux new version
SublimeText3 Linux latest version