Php reference (&) and file reference

Source: Internet
Author: User
Let's talk about two types of references: variable references and file include references. through the include () or require () function, you can insert the content of a php file into the file before the server executes the php file, except that they handle errors... let's talk about two types of references: variable references and file include references. through the include () or require () function, you can insert the content of a file into the file before the php file is executed on the server. except for the different ways in which they handle errors, the two functions are the same in other aspects. include () the function generates a warning, but the script continues to run. The require () function generates a fatal error, fatal error, which stops execution after an error occurs. the instance code is as follows:

  welcome to my home page 

some text

Three Files: "default. php", "about. php", and "contact. php" all reference the "menu. php" file. this is the code in "default. php:

  welcome to my home page 

some text

Php reference (that is, adding & symbol before variables, functions, and objects)

The reference in php means that different names access the content of the same variable, which is different from the pointer in c, the pointer in C language stores the address where the variable content is stored in the memory.

Variable reference

Php reference allows you to use two variables to point to the same content. the code is as follows:

 

Function address transfer call

I won't say much about the address transfer call. the code is provided below:

Function test (& $ a) {$ a = $ a + 100;} $ B = 1; echo $ B; // output 1 // open source code phprm.com test ($ B); // Here $ B is actually the memory address of the variable content of $ B, by changing the value of $ a in the function, you can change the value of $ B echo"
"; Echo $ B; // output 101

Note that test (1); will cause an error.

Function Reference return,First look at the code:

Function & test () {static $ B = 0; // declare a static variable $ B = $ B + 1; echo $ B; return $ B ;} $ a = test (); // This statement will output the value of $ B as 1 $ a = 5; $ a = test (); // This statement outputs the value of $ B as 2 $ a = & test (); // This statement outputs the value of $ B as 3 $ a = 5; $ a = test (); // This statement outputs a value of 6 for $ B.

The following explains:

In this way, $ a = test (); does not actually get the function reference and return, which is no different from the normal function call. The reason is: this is the PHP rule, PHP requires that $ a = & test (); is used to obtain the function reference and return.

As for what is a reference return, the PHP manual says that the reference return statement is used when you want to use a function to find the variable to which the reference should be bound, I haven't understood it for a long time.

The preceding example shows that $ a = test () is used to call a function, but the function value is assigned to $ a, and $ a makes any changes, will not affect $ B in the function.

But how to call a function through $ a = & test, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable, this is equivalent to the effect ($ a = & B;). Therefore, the value of $ a is changed and the value of $ B is changed. Therefore, the following code is executed:

$ A = & test (); $ a = 5; later, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.

The code is as follows:

 Abc; // Here abc echo $ c-> abc; // Here abc $ B-> abc = "def"; echo $ c-> abc; // output def here?>

The above code is the running effect in php5.

In php5, object replication is implemented through reference. in the above column, $ B = new a; $ c = $ B; is equivalent to $ B = new; $ c = & $ B;

In php5, the object is called by reference by default, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, php defines a special method called _ clone.

Role of reference

If the program is large, there are many variables that reference the same object, and you want to manually clear the object after it is used up, I suggest using the & method, then use the $ var = null method to clear it. In other cases, use the default php5 method. in addition, we recommend that you use the "&" method for transferring large arrays in php5, which saves memory space.

Cancel reference

When you unset a reference, you just disconnect the binding between the variable name and the variable content, which does not mean that the variable content is destroyed, for example, the following code:

 

Global Reference

When a global $ var variable is declared, a reference to the global variable is actually created, that is, it is the same as doing so. The code is as follows:

 

This means that, for example, unset $ var does not unset global variables.

$ This

In the method of an object, $ this is always a reference to the object that calls it.

Next is an episode.

In php, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the zend core. in php, the reference uses the principle of "copy at Write, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

In layman's terms:

1: if the following code exists:

$ A = "abc"; $ B = $;

In fact, both $ a and $ B point to the same memory address, instead of occupying different memory for $ a and $ B.

2: Add the following code on the basis of the above code:

$ A = "efg ";

Because the memory data pointed to by $ a and $ B needs to be re-written, the zend core will automatically determine and automatically generate a $ a data copy for $ B, apply for a new memory for storage.

Address:

Reprinted at will, but please attach the article address :-)

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.