3 ways and differences of function parameter passing in PHP (with detailed explanation)

烟雨青岚
Release: 2023-04-08 19:12:02
forward
7221 people have browsed it

3 ways and differences of function parameter passing in PHP (with detailed explanation)

Three ways and differences of passing function parameters in php

1. Pass by value ( pass by value)

Using this method, the calling function does not operate on the actual parameters. That is to say, even if the value of the formal parameter changes in the function, the value of the actual parameter It will not be affected at all and will still be the value before the call. This is equivalent to assigning a copy of the actual parameter to the formal parameter, and then recycling the memory after the call.

<span style="font-size:18px;"><strong>#include<stdio.h>  
      
    int sum(int x,int y)  
    {  
        return x+y;  
    }  
    int main()  
    {  
        int result=sum(2,3);//通过直接对sum传递参数   
        printf("%d\n",result);  
        return 0;  
    }</strong></span>
Copy after login

2. Pass by pointer

The difference between pass by address and pass by value is that it transfers the storage address of the actual parameter to the corresponding form parameter, so that the formal parameter pointer and the actual parameter pointer point to the same address. Therefore, any changes in the address pointed to by the formal parameter pointer in the called function will affect the actual parameters.

  #include<stdio.h>  
      
    int sum(int* x,int* y)  
    {  
        return *x+*y;  
    }  
    int main()  
    {  
        int a,b;  
        a=2;  b=3;  
        int result=sum(&a,&b);//通过地址对sum传递参数   
        printf("%d\n",result);  
        return 0;  
    }
Copy after login

3. Pass by reference

With a reference as a parameter, any operation on the formal parameter can change the corresponding The data makes function calls convenient and natural. The way to pass by reference is to add the reference operator "&" in front of the formal parameters when defining the function. The reference is equivalent to the alias of the actual parameter. It is the same variable or value as the actual parameter, and its change is the change of the actual parameter.

<span style="font-size:18px;"><strong>#include<stdio.h>  
    int sum(int& x,int& y)<span style="color:#ff0000;">//定义引用类型</span>  
    {  
        return x+y;  
    }  
    int main()  
    {  
        int a,b;  
        a=2;  b=3;  
        int result=sum(a,b);//通过引用对sum传递参数   
        printf("%d\n",result);  
        return 0;  
    }</strong></span>
Copy after login

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of 3 ways and differences of function parameter passing in PHP (with detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template