Discussion on some noticeable problems in PHP reference & symbols _php Tutorial

Source: Internet
Author: User
Many people misunderstand that the reference in PHP is the same as the pointer in C, which is not, in fact, very different. In addition to the pointer in the C language is not explicitly stated in the process of the array, the others need to be defined using *, and PHP's point-of-address (similar pointer) function is not implemented by the user itself, is implemented by the Zend Core, the PHP reference & symbol is the "copy-on-write" principle, Unless a write operation occurs, a variable or object that points to the same address is not copied, such as the following code:

 
  
  
  1. $ a Array(' A ', ' C ' ... ' n ');
  2. $ b

If the program only executes here, $b and $b are the same, but not like C, $a and $b occupy different memory space, but instead point to the same piece of memory, which is the difference between PHP and C, do not need to write $b=& $a to represent the $b point to $ A memory, Zend has already helped you to make references, and Zend will be very intelligent to help you decide when to deal with this and when not to do so.

If you continue to write the following code later, add a function that passes the parameters in PHP reference & notation and prints the output array size.

 
  
  
  1. Function PrintArray (& $arr)//reference delivery
  2. {
  3. Print (count ($arr));
  4. }

In the code above, we pass the $ A array into the PrintArray () function via the PHP reference & notation, and the Zend engine will think that PrintArray () may cause a change to $ A, which will automatically produce a $ A copy of the data for $b, Re-request a piece of memory for storage. This is the "copy-on-write" concept mentioned earlier.

If we change the code above to the following:

 
  
  
  1. function PrintArray ($arr) //value Pass
  2. {
  3. Print (count ($arr));
  4. }


The above code passes a $ A value directly into PrintArray () and there is no reference pass at this time, so there is no write-time copy.

You can test the execution efficiency of the two lines of code above, such as adding a loop 1000 times outside to see how time is running, and the result will let you know that incorrect use of PHP references & symbols can result in performance degradation of more than 30%.


http://www.bkjia.com/PHPjc/446367.html www.bkjia.com true http://www.bkjia.com/PHPjc/446367.html techarticle Many people misunderstand that the reference in PHP is the same as the pointer in C, which is not, in fact, very different. The pointers in the C language are not explicitly declared except in the process of passing the array, other ...

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