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
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
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);
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

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,

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++ 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.

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 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

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)

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).

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.
