Home>Article>Backend Development> Deeply understand the difference between passing by value and passing by reference in PHP

Deeply understand the difference between passing by value and passing by reference in PHP

王林
王林 Original
2019-09-04 14:34:57 3029browse

Deeply understand the difference between passing by value and passing by reference in PHP

The difference between PHP value passing and reference passing. When to pass by value and when to pass by reference

(1) Pass by value: Any changes to the value within the function scope will be ignored outside the function

(2) Pass by reference: Function scope Any changes to the internal value will also reflect these modifications outside the function

(3) Advantages and Disadvantages:

A: When passing by value, PHP must copy the value. Especially for large strings and objects, this can be a costly operation.

B. Passing by reference does not require copying the value, which is very good for improving performance.

'蝙蝠侠', 'no2'=>'超人'); $hero2 = &$hero; $hero2['no1'] = '蜘蛛侠'; echo '
'; var_dump($hero); var_dump($hero2); $a = null; $b = &$a; $b = 'abc'; var_dump($a, $b);

Recommended video tutorial:PHP video tutorial

The above is the detailed content of Deeply understand the difference between passing by value and passing by reference in PHP. For more information, please follow other related articles on the PHP Chinese website!

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