PHP reference details

Source: Internet
Author: User
This article shows you the reference of PHP in detail. It is a very good article with examples. We recommend this article for your reference.

This article shows you the reference of PHP in detail. It is a very good article with examples. We recommend this article for your reference.

Reference

Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. A reference can be considered as a hardlink in a Unix file system.

Reference

PHP references allow two variables to point to the same content. This means that when you do this:

The Code is as follows:


<? Php
$ A = & $ B;
?>

This means that $ a and $ B point to the same variable.

Note:

$ A and $ B are exactly the same here. This doesn't mean $ a points to $ B or vice versa, but $ a and $ B point to the same place.

Note:

If an array with a reference is copied, its value is not removed from the reference. This is also true for passing values from arrays to functions.

Note:

If an undefined variable is referenced, assigned, referenced, or returned, the variable is automatically created.

Example #1 Use reference for undefined Variables

The Code is as follows:


<? Php
Function foo (& $ var ){}
Foo ($ a); // $ a is "created" and assigned to null
$ B = array ();
Foo ($ B ['B']);
Var_dump (array_key_exists ('B', $ B); // bool (true)
$ C = new StdClass;
Foo ($ c-> d );
Var_dump (property_exists ($ c, 'D'); // bool (true)
?>

The same syntax can be used in functions, which return references, and in the new operator (PHP 4.0.4 and later versions ):

The Code is as follows:


<? Php
$ Bar = & new fooclass ();
$ Foo = & find_var ($ bar );
?>

Since PHP 5, new automatically returns a reference. Therefore, when = & is used here, messages at the E_STRICT level will be generated.

Note:

No & operator is used to generate a copy of the object. If $ this is used in the class, it will act on the current instance of the class. The instance (such as an object) will be copied without a value & and $ this will apply to this copy, which is not always the desired result. Due to performance and memory consumption problems, you usually only want to work on one instance.

Although the @ operator can be used to suppress any error information in the constructor, such as @ new, it does not work when the & new statement is used. This is a limitation of the Zend engine and may cause a parsing error.

Warning

If a global variable is assigned to a reference within a function, the reference is only visible within the function. You can avoid this by using the $ GLOBALS array.

Example #2 reference global variables in the function

The Code is as follows:


<? Php
$ Var1 = "Example variable ";
$ Var2 = "";
Function global_references ($ use_globals)
{
Global $ var1, $ var2;
If (! $ Use_globals ){
$ Var2 = & $ var1; // visible only inside the function
} Else {
$ GLOBALS ["var2"] = & $ var1; // visible also in global context
}
}
Global_references (false );
Echo "var2 is set to '$ var2' \ n"; // var2 is set''
Global_references (true );
Echo "var2 is set to '$ var2' \ n"; // var2 is set to 'example variable'
?>

Use global $ var as the abbreviation of $ var = & $ GLOBALS ['var. Therefore, assigning other references to $ var only changes the reference of local variables.
Note:

If you assign a value to a referenced variable in the foreach statement, the referenced object is also changed.

Example #3 references and foreach statements

The Code is as follows:


<? Php
$ Ref = 0;
$ Row = & $ ref;
Foreach (array (1, 2, 3) as $ row ){
// Do something
}
Echo $ ref; // 3-last element of the iterated array
?>

The second thing to do with referencing is to use referencing to pass variables. This is achieved by creating a local variable in the function and referencing the same content in the call range. For example:

The Code is as follows:


<? Php
Function foo (& $ var)
{
$ Var ++;
}

$ A = 5;
Foo ($ );
?>

Changes $ a to 6. This is because the variable $ var points to the same content as $ a in the foo function. For more detailed explanations, refer to transfer references.

The third thing that references is to return a reference.

Reference is not

As mentioned above, the reference is not a pointer. This means that the following structure will not produce the expected results:

The Code is as follows:


<? Php
Function foo (& $ var)
{
$ Var = & $ GLOBALS ["baz"];
}
Foo ($ bar );
?>

This binds the $ var variable in the foo function to $ bar when calling the function, but is then re-bound to $ GLOBALS ["baz. It is impossible to bind $ bar to another variable within the function call range through the reference mechanism, because there is no variable $ bar in function foo (it is represented as $ var, but $ var only contains the variable content and does not call the name-to-value binding in the symbol table ). You can use the reference return to reference the variable selected by the function.

Reference Transfer

A variable can be passed to the function through reference, so that the function can modify the value of its parameter. Syntax:

The Code is as follows:


<? Php
Function foo (& $ var)
{
$ Var ++;
}
$ A = 5;
Foo ($ );
// $ A is 6 here
?>

Note that there is no reference symbol when calling a function-only in the function definition. Function Definition is enough to make parameters pass through references correctly. In the latest PHP version, if you use & in foo (& $ a);, you will receive a warning that "Call-time pass-by-reference" is out of date.

The following content can be passed through reference:

Variable, such as foo ($)
New statement, such as foo (new foobar ())
Reference returned from the function, for example:

The Code is as follows:


<? Php
Function & bar ()
{
$ A = 5;
Return $;
}
Foo (bar ());
?>

For more information, see Return references.
No other expressions can be passed through references, and the results are undefined. For example, the example passed by reference below is invalid:

The Code is as follows:

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.