Home Backend Development PHP Problem How to pass parameters to php function

How to pass parameters to php function

Sep 26, 2019 am 11:51 AM
parameter

How to pass parameters to php function

Function parameter passing in php has formal parameter passing (borrowing the term from programming languages ​​such as c language).

That is, assigning the value of the variable to the parameter of the function. All operations performed on the function parameter have nothing to do with the original variable.

//php
function testX( $b )
{
     return $b-=2;
}
$a = 5 ;
$c = testX($a);
print_r($a);//5
print_r($c);//3
Copy after login

And address transfer (reference transfer) is to give the address of the variable to the parameter of the function. All operations performed on the parameters in the function are equivalent to operations on the variables.

//php
function testS( &$b )
{
     return $b-=2;
}
$a = 5 ;
$c = testX($a);
print_r($a);//3
print_r($c);//3
Copy after login

In PHP, function parameters and variables will all open up space in the memory, but normal parameter passing assigns values ​​​​to parameters, while reference passing does not, it will put the address of the variable

Go to the space where the parameters are located.

It is particularly noteworthy that the writing method of reference passing has been changed after PHP5. As shown in the above code, change the way PHP is written to the same way as other programming languages. You need to

use & when defining, and you don’t need to use & when calling. It is very different from the previous writing method.

//php以前的写法
function &testS( &$b )
{
 return $b-=2;
}
$c = &testS($a);
Copy after login

Advantages:

Personally, I think this change in writing is to reduce the uniqueness of the php language and move closer to the writing methods of other programming languages. In a sense, it allows programmers who are familiar with other programming languages ​​​​to master PHP faster. From the perspective of code reading and logical understanding, it completely ignores all operations performed on variables within the function. It makes more sense for code abstraction.

Disadvantages:

I think for programmers on the front line of development, this is a crazy change. Especially for those doing secondary development. Secondary developers need to develop on the basis of predecessors. When seeing functions, I am used to distinguishing reference-by-value functions with &. Without this writing method, all functions are the same. If a call to a reference-by-value function is involved, the programmer needs to go inside the function to check the code details. Especially when there is an error in the program. You need to understand in detail all the changes made to variables by the reference-by-value function. And canceling the & to call the reference function means that you need to check all the functions called by the program (because you don't know which function is the reference function), and the workload can be imagined!

There are also global variables (global). This is a direct and fast way to define parameters. Set the parameters to be used by the function as global variables. In this way, operations inside the function directly use the variable itself.

There is no problem in using it in a short program, but this is not an ideal way to define parameters when calling external files!

The above is the detailed content of How to pass parameters to php function. 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)

New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods Jul 29, 2023 pm 09:19 PM

New feature of PHP5.4 version: How to use callable type hint parameters to accept callable functions or methods Introduction: PHP5.4 version introduces a very convenient new feature - you can use callable type hint parameters to accept callable functions or methods . This new feature allows functions and methods to directly specify the corresponding callable parameters without additional checks and conversions. In this article, we will introduce the use of callable type hints and provide some code examples,

What do product parameters mean? What do product parameters mean? Jul 05, 2023 am 11:13 AM

Product parameters refer to the meaning of product attributes. For example, clothing parameters include brand, material, model, size, style, fabric, applicable group, color, etc.; food parameters include brand, weight, material, health license number, applicable group, color, etc.; home appliance parameters include brand, size, color , place of origin, applicable voltage, signal, interface and power, etc.

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ parameter type safety checking ensures that functions only accept values ​​of expected types through compile-time checks, run-time checks, and static assertions, preventing unexpected behavior and program crashes: Compile-time type checking: The compiler checks type compatibility. Runtime type checking: Use dynamic_cast to check type compatibility, and throw an exception if there is no match. Static assertion: Assert type conditions at compile time.

PHP Warning: Solution to in_array() expects parameter PHP Warning: Solution to in_array() expects parameter Jun 22, 2023 pm 11:52 PM

During the development process, we may encounter such an error message: PHPWarning: in_array()expectsparameter. This error message will appear when using the in_array() function. It may be caused by incorrect parameter passing of the function. Let’s take a look at the solution to this error message. First, you need to clarify the role of the in_array() function: check whether a value exists in the array. The prototype of this function is: in_a

i9-12900H parameter evaluation list i9-12900H parameter evaluation list Feb 23, 2024 am 09:25 AM

i9-12900H is a 14-core processor. The architecture and technology used are all new, and the threads are also very high. The overall work is excellent, and some parameters have been improved. It is particularly comprehensive and can bring users Excellent experience. i9-12900H parameter evaluation review: 1. i9-12900H is a 14-core processor, which adopts the q1 architecture and 24576kb process technology, and has been upgraded to 20 threads. 2. The maximum CPU frequency is 1.80! 5.00ghz, which mainly depends on the workload. 3. Compared with the price, it is very suitable. The price-performance ratio is very good, and it is very suitable for some partners who need normal use. i9-12900H parameter evaluation and performance running scores

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

Can't a language model with 10 billion parameters run? A Chinese doctor from MIT proposed SmoothQuant quantification, which reduced memory requirements by half and increased speed by 1.56 times! Can't a language model with 10 billion parameters run? A Chinese doctor from MIT proposed SmoothQuant quantification, which reduced memory requirements by half and increased speed by 1.56 times! Apr 13, 2023 am 09:31 AM

Although large-scale language models (LLM) have strong performance, the number of parameters can easily reach hundreds of billions, and the demand for computing equipment and memory is so large that ordinary companies cannot afford it. Quantization is a common compression operation that sacrifices some model performance in exchange for faster inference speed and less memory requirements by reducing the accuracy of model weights (such as 32 bit to 8 bit). But for LLMs with more than 100 billion parameters, existing compression methods cannot maintain the accuracy of the model, nor can they run efficiently on hardware. Recently, researchers from MIT and NVIDIA jointly proposed a general-purpose post-training quantization (GPQ).

Advanced usage of reference parameters and pointer parameters in C++ functions Advanced usage of reference parameters and pointer parameters in C++ functions Apr 21, 2024 am 09:39 AM

Reference parameters in C++ functions (essentially variable aliases, modifying the reference modifies the original variable) and pointer parameters (storing the memory address of the original variable, modifying the variable by dereferencing the pointer) have different usages when passing and modifying variables. Reference parameters are often used to modify original variables (especially large structures) to avoid copy overhead when passed to constructors or assignment operators. Pointer parameters are used to flexibly point to memory locations, implement dynamic data structures, or pass null pointers to represent optional parameters.

See all articles