The difference between a value in PHP and a pass-through reference

Source: Internet
Author: User
This article mainly introduces the invocation of PHP in the difference between the value and the reference, has a certain reference value, now share to everyone, the need for friends can refer to

PHP Transfer Value : Within the scope of the function, changing the value of a variable is not likely to affect the values of the variables outside the function.
PHP by reference : Within the scope of the function, any change to the value is also reflected outside the function, because the pass-through refers to the memory address.




Daniel's Explanation:

Pass value : Same as copy. "For example, I have a Yikanghuayuan house, I give you building materials, you built a root of my house the same house, you do nothing in your house will affect me, I do in my house do not affect you, independent of each other." "



[PHP] view plain copy

    1. <?php      $testa =1;               Define variable a $testb =2;               Define variable b    $testb = $testa;       Variable a assigns a value to the variable B    echo $testb;            Display as 1    ?>

reference : similar to the C language of the pointer, feel the same. For example, I have a Yikanghuayuan house, I give you a key, two of us can enter this House, what you do in the House will affect me.



[PHP] view plain copy

    1. <?php      $param 2=1;               Define variables 2    $param 1 = & $param 2;      Pass a reference to variable 2 to the variable 1    echo $param 2;            Shown as 1    $param 1 = 2;             Assign 2 to the variable 1    echo $param 2;            Display as 2    ?>


"Pros and Cons:" It takes time to pass a value, especially for large strings and objects, which can be a costly operation, delivering a reference, and any operation within a function is equivalent to the operation of a transfer variable, and high efficiency when transferring large variables!


Reprint please specify the source https://blog.csdn.net/qq_28194557/article/details/70543753

Related recommendations:

An explanation of anonymous and closure functions in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.