Notes on the use of Reference & in PHP for Dynamic Web pages

Source: Internet
Author: User

A friend of the previous post mentioned this question, here to tidy up, hoping to make more new PHP friends get help.

PHP references (that is, the variables or functions, objects, etc. before the addition of & symbol) is a high-level topic, the novice more attention, the correct understanding of PHP reference is very important to performance has a greater impact, and understand the error may lead to program errors!

Many people misunderstand that the reference in PHP is the same as the pointer in C, which is actually not the case, and it's a big difference. The pointer in C language is not explicitly declared except in the array pass. All others need to use * to define, and PHP for the address (like a pointer) function is not implemented by the user, is implemented by the Zend Core, PHP referenced by the "write-time copy" principle, Is that unless a write occurs, a variable or object that points to the same address is not copied, such as the following code:

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

If the program only executes here, $b and $b are the same, but not like C, $a and $b occupy different memory space, but point to the same memory, which is the difference between PHP and C, do not need to write a $b=& $a to represent the $b point to $a memory, Zend has helped you implement the citation, and Zend will be very intelligent to help you determine when to do so and when to do so.

If you continue to write the following code later, add a function, pass the argument by reference, and print out the array size.

Function PrintArray (& $arr)//reference delivery
{
Print (count ($arr));
}
PrintArray ($a);

In the code above, we pass the $a array to the PrintArray () function, and the Zend Engine thinks that printarray () can cause a change to the $a, which automatically produces a $b copy of the data for $a and then requests a new memory for storage. This is the "write-time copy" concept mentioned earlier.

If we change the above code to the following:

function PrintArray ($arr)//value delivery
{
Print (count ($arr));
}
PrintArray ($a);

The above code passes the $a value directly to PrintArray (), there is no reference passing, so no write-time copy appears.

You can test the performance of the above two lines of code, such as the outside to join a loop 1000 times, to see how time-consuming the operation, the results will let you know that incorrect use of the reference will lead to a decrease in performance by more than 30%.



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.