PHP variable scope, reference, object reference, pass _php tutorial

Source: Internet
Author: User
This article describes the scope of PHP variables, references, object references, delivery, there is a need for friends to refer to.

Variable Range
The scope of a variable is the context in which it is defined (that is, its effective scope). Most PHP variables have a single range. This separate scope span also contains the files introduced by include and require. For example:

The code is as follows Copy Code
$a = 1;
Include ' B.inc ';
?>

Here the variable $a will take effect in the include file B.inc. However, in a user-defined function, a local function scope is introduced. Any variables that are used inside the function will be limited to the local function in the default context. For example:

The code is as follows Copy Code

$a = 1; /* Global scope */

function Test ()
{
echo $a; /* Reference to local scope variable */
}

Test ();
?>

This script does not have any output, because the Echo statement refers to a local version of the variable $a, and within that range, it is not assigned a value. You may notice that the global variables in PHP are a little different from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. This can cause some problems, and some people may accidentally change a global variable. In PHP, global variables must be declared as globals when used in functions.


The reference in PHP means: Different names access the same variable content.
There is a difference between the pointers in the C language. In the C language, the pointer stores the address of the variable's contents in memory.
A reference to a variable

The code is as follows Copy Code
$a = "ABC";
$b =& $a;
echo $a; echo "RN";//output here: ABC
echo $b; echo "RN";//output here: ABC
$b = "EFG";
echo $a, echo "RN";//The value of $ A here becomes EFG so the output EFG
echo $b, echo "RN";//Output EFG here
?>

Invocation of a function's address
Address call I'm not going to say much. Directly below the code

The code is as follows Copy Code
Function test (& $a)
{
$a = $a +100;
}
$b = 1;
echo $b;//Output 1
echo "
";
Test ($b); Here $b passed to the function is actually the memory address of the variable content of $b, by changing the value of $ A in the function can change the value of $b
echo "
";
echo $b;//Output 101
?>

It is important to note that here Test (1), the words will be wrong, reason to think
A reference to the function returns
Look at the code first

copy code

"!--? php
function &test ()
{static $b =0;//Declare a static variable
$b = $b +1;
Echo $b;
return $b;
}
$a =test ();//This statement outputs a value of $b of 1
Echo "
";
$a = 5;
$a =test ();//This statement outputs a value of $b of 2
Echo "
";
$a =&test ();//This statement outputs a value of $b of 3
echo "
";
$a = 5;
$a =test ();//This statement outputs a value of $b of 6
.

The following is explained below:
$a=test () In this way, the result is not the function of the reference return, which is not the same as the normal function call the reason: this is the PHP provisions. The
PHP rule is passed $a=&test (); the way to get is to return the reference to the function.
As to what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) I didn't read this shit for a long while.
using the example above to explain is
$a =test () call the function, just assign the value of the function to $ A, and $ A does not affect $b
in the function and call the function by $a=&test (). , his role is to refer to the memory address of the $b variable in the return $b to the same place as the memory address of the $ A variable
that produces the equivalent effect ($a =&b;) So changing the value of $ A also changes the value of $b so that the
$a =&test () is executed;
$a = 5;
later, the value of the $b is changed to 5
This is to let everyone understand that the function's reference is returned to use a static variable, but the function's reference returns a reference to the
object in the object

The code is as follows Copy Code

Class a{
Public $ABC = "ABC";
}
$b =new A;
$c = $b;
echo $b->abc;//here output ABC
echo $c->abc;//here output ABC
$b->abc= "DEF";
echo $c->abc;//here output def
?>

The above code is the result of running in PHP5
The replication of objects in PHP5 is done by reference. The above $b=new A; $c = $b; is actually equivalent to $b=new A; $c =& $b;
The default in PHP5 is to invoke the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.
such as the following example

The code is as follows Copy Code

Class a{
Public $ABC = "ABC";
}
$b =new A;
$c = $b;
$d = clone$b;
echo $b->abc;//here output ABC
echo $c->abc;//here output ABC
$b->abc= "DEF";
echo $c->abc;//here output def
echo $b->abc;//here output def
$d->abc= "111";
echo $d->abc;//here output 111
echo $b->abc;//here output Def, stating that the copy $d object after clone does not affect $b object
?>

The role of references
If the program is larger, referencing the same object is more variable, and you want to use the object after the manual removal of it, the personal suggestion "&" method, and then $var=null to clear. At other times, it's the default way to use PHP5. In addition, the PHP5
For large arrays of delivery, it is recommended to use the "&" method, after all, save memory space usage.

Dereference
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:

The code is as follows Copy Code
$a = 1;
$b = & $a;
unset ($a);
Var_dump ($a);//output NULL here
Var_dump ($b);//output int 1 here
?>

Not unset $b, just $a.

Global references
When you declare a variable with the global $var, you actually establish a reference to the global variable. In other words, it is the same as doing this:
$var =& $GLOBALS ["var"];
?>
This means, for example, that the unset $var does not unset global variables.
$this
In the method of an object, $this is always a reference to the object that called it.

Here's a little episode.
PHP in the direction of the address (similar to the pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "copy-on-write" principle, that is, unless a write operation occurs, the variable or object pointing to the same address is
will not be copied.
The popular Speaking
1: If you have the following code
$a = "ABC";
$b = $a;
In fact, $a and $b both point to the same memory address and not $ A and $b occupy different memory
2: If you add the following code based on the above code
$a = "ABC";
$b = $a;
$a = "EFG";
Since $ A and $b point to the memory of the data to be re-written once, at this time Zend core will automatically decide to automatically produce a $ A copy of the data for $b, re-request a piece of memory for storage

PHP Learning Variable Pass!

1 Passing variables with URLs

Instance

The code is as follows Copy Code



My favorite MovieSite


echo "My favorite movie site is:";
echo $_get[' Favmovie '];
echo "
";
$movierate = 5;
echo "My favorite movie rating for this movie is";
Echo $movierate;
?>

Save this file as movie1.php.

Use $_get[' Favmovie '] to receive URL-passed variables!

Write another file to save as

moviesite.php

The code is as follows Copy Code



Find My favorite Movie


$myfavmovie =urlencode ("Life of Brian");
echo "";
echo "Click here to see more information about my favorite movie!";
echo "";
?>

http://www.bkjia.com/PHPjc/629033.html www.bkjia.com true http://www.bkjia.com/PHPjc/629033.html techarticle This article describes the scope of PHP variables, references, object references, delivery, there is a need for friends to refer to. The scope of a variable range variable is the context in which it is defined (that is, its ...).

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