Home Backend Development PHP Tutorial 3 ways and differences of function parameter passing in PHP (with detailed explanation)

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

Jun 08, 2020 am 10:57 AM

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!

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

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles