Considerations for referencing and using PHP in dynamic web pages

Source: Internet
Author: User
A friend's post mentioned this question. I would like to brief it here to help more new php students. Php reference (that is, adding symbols before variables, functions, and objects) is an advanced topic.

A friend's post mentioned this question. I would like to brief it here to help more new php students.

Php reference (that is, adding & symbol before variables, functions, and objects) is an advanced topic. it is important for new users to pay more attention and understand php references accurately, it has a great impact on performance and understands that errors may lead to program errors!

Many people misinterpret the reference in php as the pointer in C. in fact, this is not the case and it is quite different. In C language, pointers do not need to be explicitly known in the array transfer process, but must be defined by the application *. in php, pointers to addresses (similar to pointers) the functions are not implemented by the user, but are implemented by the Zend core. the reference in php adopts the principle of "copy upon writing", that is, unless the write control is generated, variables or objects pointing to the same address are not copied, for example, the following code:

$ A = array ('A', 'C'... 'n ');
$ B = $;

If the program only performs here, $ B and $ B are the same, but not like C, $ a and $ B occupy different memory space, it points to the same memory, which is the difference between php and c. it does not need to be written as $ B = & $ a to show that $ B points to $ a memory, zend has already helped you implement the reference, and zend will be very intelligent to help you determine when it should be handled and when it should not.

If you write the following code at the end, add a function, pass the parameters by referencing the method, and print the output array size.

Function printArray (& $ arr) // reference transfer
{
Print (count ($ arr ));
}
PrintArray ($ );

In the above code, we pass the $ a array into the printArray () function by referencing it. the zend Engine will think that printArray () may lead to a change to $, at this time, a $ a data copy is automatically produced for $ B, and a memory is re-applied for storage. This is the "copy at write time" concept mentioned above.

Suppose we change the above code to the following:

Function printArray ($ arr) // value transfer
{
Print (count ($ arr ));
}
PrintArray ($ );

The above code directly transmits the $ a value to printArray (). at this time, there is no reference transfer, so it does not present the copy at write time.

You can test the performance efficiency of the above two lines of code. for example, if you participate in a loop 1000 times outside, you can check the running time, the results will let you know that inaccurate application references will cause performance to fall above 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.