What are the types of parameter assignments in php functions?
There are three types of parameter assignment in PHP functions: 1. Value-passing assignment, copying the value of the actual parameter and assigning it to the formal parameter of the function; 2. Reference-passing assignment, copying the memory address of the actual parameter One copy is then passed to the formal parameter of the function, and then the actual parameter value is assigned to the formal parameter; 3. Directly specify the default value for the parameter of the function, the syntax is "function name (parameter variable = 'value')".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The parameter assignment of the php function is 3 Type:
1. Value transfer assignment
2. Reference transfer assignment
3.Default parameter value
The following will give you a detailed introduction .
1. Value passing assignment
Value passing is the default value passing method for functions in PHP, also known as "copy passing by value".
As the name suggests, the value assignment method will copy the value of the actual parameter and then pass it to the formal parameter of the function, so operating the value of the parameter in the function will not affect the actual parameters outside the function.
So if you don’t want the function to modify the value of the actual parameter, you can pass it by value.
Example:
<?php header(&#39;content-type:text/html;charset=utf-8&#39;); function swap($a, $b){ echo &#39;函数内,交换前 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;; $temp = $a; $a = $b; $b = $temp; echo &#39;函数内,交换后 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;; } $x = 5; $y = 7; echo &#39;函数外,交换前 $x = &#39;.$x.&#39;, $y = &#39;.$y.&#39;<br>&#39;; swap($x, $y); echo &#39;函数外,交换后 $x = &#39;.$x.&#39;, $y = &#39;.$y; ?>
2. Pass-by-reference assignment
Passing parameters by reference means passing the actual parameters Make a copy of the memory address and pass it to the formal parameter of the function, and then assign the actual parameter value to the formal parameter.
Both the actual parameters and the formal parameters point to the same memory address, so the function's operation on the formal parameters will affect the actual parameters outside the function.
Passing by reference is to pass the memory address of the actual parameter to the formal parameter of the function. Therefore, the actual parameters and formal parameters point to the same memory address. At this time, all operations inside the function will affect the values of the actual parameters outside the function. The method of passing by reference is to add a &
symbol on the basis of value passing, as shown below:
function name (&参数1, &参数2, ..., &参数3) { ... }
Example:
<?php header(&#39;content-type:text/html;charset=utf-8&#39;); function swap(&$a, &$b){ echo &#39;函数内,交换前 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;; $temp = $a; $a = $b; $b = $temp; echo &#39;函数内,交换后 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;; } $x = 5; $y = 7; echo &#39;函数外,交换前 $x = &#39;.$x.&#39;, $y = &#39;.$y.&#39;<br>&#39;; swap($x, $y); echo &#39;函数外,交换后 $x = &#39;.$x.&#39;, $y = &#39;.$y; ?>
3. Default parameter value
The default parameter is to specify a default value for one or more formal parameters of the function. If the corresponding parameter is not passed in when calling the function value, then the function will use this default value, which can avoid errors when calling without parameters, and can also make some programs more reasonable. If the corresponding parameters are passed in, this default value will be replaced.
The default parameters of the function are as follows:
function name ($str = &#39;默认值&#39;, $url) { echo $str; }
You need to use =
to connect the default values of the shape participants.
Example:
<?php function add($a, $b=56){ echo $a.&#39; + &#39;.$b.&#39; = &#39;.($a+$b).&#39;<br>&#39;; } add(11); add(37, 29); ?>
The default parameters can also be multiple, and the default parameters must be placed to the right of the non-default parameters, and The value of the specified default parameter must be a specific value, such as a number or a string, and cannot be a variable.
<?php function add($a, $b=33, $c=57){ echo $a.&#39; + &#39;.$b.&#39; + &#39;.$c.&#39; = &#39;.($a+$b+$c).&#39;<br>&#39;; } add(11); add(31, 22); add(64, 9, 7); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the types of parameter assignments in php functions?. 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

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

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.