The functions in PHP look very simple, but in fact they are very powerful. I can divide them into the following three categories according to whether the function name is fixed:
1. Functions with fixed names:
This type of function, also called: regular function, is created directly using the keyword function, which is also the most familiar type to everyone, for example:
<?php //1按名调用:最直接和普通的方式echo '1.用function关键字创建常规函数,按函数名称调用:<br>'; function add($a, $b){return $a.'+'.$b.'='.($a+$b);}; echo add(5,6); echo '<hr>';
Execution returns:
1.用function关键字创建常规函数,按函数名称调用: 5+6=11
2. Name Dynamically set functions:
This type of function has many names, such as: anonymous function, variable function, variable function, closure function, etc. In fact, they are all the same. Don’t be intimidated by the name.
This kind of dynamically set function is called using the variable name that references this function, so it is very suitable for use in callback functions~~
For teaching purposes, I will collectively refer to it here as: Anonymous function.
There are two main ways to create anonymous functions. Some textbooks talk about them separately, so I will separate them here.
1. Creation method one: Use PHP’s built-in create_function (parameter list, function body) function to complete. This function has two parameters. These two parameters must be placed in quotation marks. Don’t ask why, that’s how PHP is. If you are not satisfied with the regulations, then you can create a programming language~~
//2.用系统函数create_function()来创建一个匿名函数/可变函数/变量函数/闭包函数echo '2.用内置create_function()函数创建匿名函数,用变量名调用:<br>'; $func1 = create_function('$a,$b','return $a.\'+\'.$b.\'=\'.($a+$b);'); echo $func1(5,6); echo '<hr>';
Execution returns:
2.用内置create_function()函数创建匿名函数,用变量名调用: 5+6=11
2. Creation method two: Use the function keyword to create, the creation process is the same as the routine The function is the same, the difference is that it appears in the position of the value, that is, on the right side of the '=' equal sign. Assign the entire created function to a variable;
//3匿名函数:也叫闭包函数,将函数做为值赋给一个变量echo '3.用function关键字创建匿名函数,用变量名调用:<br>'; $func2 = function ($a, $b){return $a.'+'.$b.'='.($a+$b);}; echo $func2(5,6); echo '<hr>';
Execution returns:
3.用function关键字创建匿名函数,用变量名调用: 5+6=11
3. There is no function name. After creation, directly pass parameters and call execution:
This type Functions are similar to anonymous functions, but are more crude. Once created, they are wrapped in parentheses and passed in parameters before being called. Therefore, this type of function does not need a name at all and is a one-time function. Just like a disposable item, throw it away after use.
//4自执行函数:创建完立即执行不必明示调用 echo '4.用function关键字创建匿名函数,然后直接传参调用执行:<br>'; echo (function ($a,$b){return $a.'+'.$b.'='.($a+$b);})(5,6); //自运行的匿名函数
Execution returns:
4.用function关键字创建匿名函数,然后直接传参调用执行: 5+6=11
Summary (very important, must read):
In php, the use of functions is still very flexible, but no matter what, functions, In the final analysis, it is still a process. At the same time, there must be a return value so that users can perceive its existence and its value!
So, a function is always used as a value. Therefore, it cannot appear on the left side of the equal sign '='. This is the biggest difference between functions and language structures.
For example, we often say: echo() and list() are a language structure, not a function. Although they look like functions, it is because they can be placed on the left side of the equal sign and are acceptable. Assignment. But functions don't work. I hope this article will give you a deeper understanding of functions~~
The above is the detailed content of Types and calling methods of PHP functions revealed. For more information, please follow other related articles on the PHP Chinese website!

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


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

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools
