Home Backend Development PHP Problem What is an anonymous function in PHP? How is it different from ordinary functions? (detailed explanation and examples)

What is an anonymous function in PHP? How is it different from ordinary functions? (detailed explanation and examples)

Jun 07, 2021 pm 05:59 PM
anonymous function

The previous article introduced you to "What is a recursive function in PHP? What are the basic elements? What is its purpose? (Attached code) 》, this article continues to introduce to you what is a recursive function in PHP? What are the basic elements? What is its purpose? (Code attached) This article will give you different gains. Let’s continue to explore the mysteries of PHP together! ! !

What is an anonymous function in PHP? How is it different from ordinary functions? (detailed explanation and examples)

#What is an anonymous function?

If you declare a function and the function does not have a function name, then the function is an anonymous function

Usage:

Use variables to receive anonymous functions

Variable name=

 function (){
Copy after login

Function body:

}; (Note that there must be points here No. ends, because anonymous functions belong to expressions)

We use code as an example: (ordinary function)

<?php
function demo(){
    echo &#39;我不想上班&#39;;
}
demo ();
?>
Copy after login

Code explanation:

First define a Ordinary function function demo(); then outputs (echo) a string of strings, and then we call the function by adding () to the function name. Running this code will get the content we want to output. The code demonstration results are as follows:

What is an anonymous function in PHP? How is it different from ordinary functions? (detailed explanation and examples)

Similarly, we first define a function function that outputs (echo) a string; then when we run it, we find that an error will be reported, and the end of the file is not found. We defined An anonymous function cannot be called because it has no name, so it does not belong to a function, it belongs to a type, but if we add a semicolon at the end, the running result will find that it will not report an error, but although our running result does not report an error, But we can't call it, thinking that the function is not named, so we need to assign it to a variable. After we assign the value, we can call it through the variable function;

(code of anonymous function Demo)

<?php
function demo(){
    echo &#39;我不想上班&#39;;
}
demo ();
$test = function(){
echo &#39;只想在家呆着&#39; ;
};
//变量函数 
$test();
?>
Copy after login

The code demonstration results are as follows:

What is an anonymous function in PHP? How is it different from ordinary functions? (detailed explanation and examples)

The above case is an anonymous function.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is an anonymous function in PHP? How is it different from ordinary functions? (detailed explanation and examples). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Usage and characteristics of C++ anonymous functions Usage and characteristics of C++ anonymous functions Apr 19, 2024 am 09:03 AM

An anonymous function, also known as a lambda expression, is a function that does not specify a name and is used for one-time use or to pass a function pointer. Features include: anonymity, one-time use, closures, return type inference. In practice, it is often used for sorting or other one-time function calls.

Analysis of anonymous function application scenarios in Golang functions Analysis of anonymous function application scenarios in Golang functions May 16, 2023 pm 10:51 PM

As a modern programming language, Golang (also known as Go language) has many powerful features. Among them, anonymous functions are a very important concept in Golang and are widely used in various scenarios. In this article, we will deeply analyze the application scenarios of anonymous functions in Golang functions. Event Handler In event handler, anonymous function is a very convenient and practical tool. Custom logic can be passed to the event handler through an anonymous function, such as: funcmain(){bt

How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? Oct 21, 2023 am 10:21 AM

How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? Before PHP7, we often used functions to encapsulate a specific piece of logic, and then called these functions in the code to implement specific functions. However, sometimes we may need to define some temporary logic blocks in the code. These logic blocks do not need to create an independent function, and at the same time, we do not want to introduce too many global variables into the code. PHP7 introduces anonymous functions and closures, which can solve this problem very well. An anonymous function is a function without a name

Can Golang anonymous functions return multiple values? Can Golang anonymous functions return multiple values? Apr 13, 2024 pm 04:09 PM

Yes, anonymous functions in Go language can return multiple values. Syntax: func(arg1,arg2,...,argN)(ret1,ret2,...,retM){//Function body}. Usage: Use the := operator to receive the return value; use the return keyword to return multiple values.

How to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic? How to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic? Oct 24, 2023 am 10:30 AM

How to use PHP7’s anonymous functions and closures to achieve more flexible and reusable code logic? In the world of PHP programming, anonymous functions and closures are very valuable and powerful tools. PHP7 introduces some new language features that make using anonymous functions and closures more convenient and flexible. This article will introduce how to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic, and provide some specific code examples. 1. Anonymous function An anonymous function is a function without a name. In PHP, you can use anonymous

Anonymous functions in PHP8.0 Anonymous functions in PHP8.0 May 14, 2023 am 08:31 AM

PHP8.0 is the latest version of the PHP programming language. One important update is improvements and enhancements to anonymous functions. An anonymous function (also called a closure) is a special type of function that can be created dynamically at runtime and passed to other functions or stored in a variable. In PHP, anonymous functions are crucial for advanced programming and web development. PHP8.0 provides some new syntax and features that make anonymous functions more flexible and easier to use. Some of the updates are as follows: Type declarations for function parameters in PHP8.0,

Python Lambda Expressions: Making Programming Easier Python Lambda Expressions: Making Programming Easier Feb 19, 2024 pm 09:54 PM

A python Lambda expression is a small anonymous function that stores an expression in a variable and returns its value. Lambda expressions are often used to perform simple tasks that can be accomplished by writing a separate function, but Lambda expressions can make the code more concise and readable. The syntax of a Lambda expression is as follows: lambdaarguments: expressionarguments is the parameter list received by the Lambda expression, and expression is the body of the Lambda expression, which contains the code that needs to be executed. For example, the following Lambda expression adds two numbers and returns their sum: lambdax,

What is the difference between lambda expressions and anonymous functions? What is the difference between lambda expressions and anonymous functions? Apr 17, 2024 pm 03:18 PM

Lambda expressions and anonymous functions are both ways of creating anonymous functions in Python, but there are differences. Assignment method: lambda expression returns a function, and anonymous functions must be assigned to variables to be used. Code complexity: A lambda expression can only contain one expression, while an anonymous function can contain multiple statements.

See all articles