PHP variable usage in php learning Notes

Source: Internet
Author: User
Variables are essential in php programming. in php, variables are classified into global variables and private variables. next I will share some of my understanding and usage of php variables, for more information, see. if you define variables and constants, how many aspects will you pay attention? You may think... variables are essential in php programming. in php, variables are classified into global variables and private variables. next I will share some of my understanding and usage of php variables, for more information, see.

If you define variables and constants, how many aspects will you pay attention? You may think:

• How to Define variables? what is the difference between variables and C?

• Are variables case sensitive?

• Are there other important PHP variables?

• Are constants and variables defined in the same way?

Let's talk about it separately.

1. how to define a variable? what is the difference between it and C?

Variables in PHP are represented by a dollar sign followed by a variable name. the variable name is case sensitive. for example:

The instance code is as follows:

 

You may also care about variable naming, which is actually the same as most languages.

2. are variables case sensitive?

As stated in 1, it is case sensitive.

Note: Since PHP4, the concept of reference assignment has been introduced, which is similar to that of most languages. However, I think C/C ++ is the most similar. because it also uses the "&" symbol.

The instance code is as follows:

 

Like other languages, you can only reference variables with variable names.

To put it bluntly, the variable in php parses the value of a variable into a variable name and reads the value of that variable name.

The instance code is as follows:

 "; // Output Chinaecho $ ."
"; // Output I'm Chinese -- the $ symbol $ a =" f "must be added in front of the variable to be parsed "; // change the name to which the variable points (here is the application of the variable) echo $."
"; // Output B $ a =" B "after pointing to the variable f above; // same as echo $ ."

"; // Output aecho $ B ."
"; // Output aecho $ B ."
"; // Output becho $ B ."

"; // Output aecho $ f ."
"; // Output becho $ f ."
"; // Output aecho $ f ."
"; // Output becho $ f ."

"; // Output a $ a =" China "; // the last variable that has been changed to B is called $ a = $ B, that is, the value of changed $ B echo $ B."
"; // Output Chinaecho $ B; // output I'm Chinese?>

Note: Variable variables cannot be applied to $ this and Super global variables (the scope of php variables is different from that of other advanced programming languages. check the code)

The instance code is as follows:

 "; // Output abcecho $ abc; // output defecho"
"; Function show () {global $ name; // Here, global is not set as a global variable. echo $ name is referenced ."
"; // Output man} function showtwo () {// global $ name; // echo $ name ."
"; Echo $ GLOBALS ['name']; // Super global variable array} show (); showtwo ();?>

Variable functions:

The instance code is as follows:

 

A typical application of variable variables:

The instance code is as follows:

   
  Untitled Document            

$ Value) {// print_r ($ _ POST); $ key = $ value;} // extract ($ _ POST ); // import the variable from the array to the current symbol table -- search for the php manual echo $ name."
"; Echo $ pwd ."
"; Echo $ tag ."
";?>

Variable scope.

Variable range

The scope of a variable is the context defined by it ). most PHP variables only have a single range. this independent range span also contains the files introduced by include and require. example:

The instance code is as follows:

$ A = 1;

Include "B. inc ";

?>

Here the variable $ a will include file B. takes effect in inc. however, in user-defined functions, a local function range will be introduced. by default, any variable used in the function will be restricted within the range of a local function. example:

The instance code is as follows:

 

This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. you may notice that the global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. this may cause some problems. some may carelessly change a global variable. global variables in PHP must be declared as global when used in functions.

The global keyword

First, an example of using global:

Example 1. use global

The instance code is as follows:

 

The output of the above script will be "3 ". specify the global variables $ a and $ B in the function. all referenced variables of any variable point to the global variable. PHP has no limit on the maximum number of global variables that a function can declare.

The second way to access variables within the global scope is to use a special PHP custom $ GLOBALS array. the previous example can be written:

Example 2. replace global with $ GLOBALS

The instance code is as follows:

 

In the $ GLOBALS array, each variable is an element. The key name corresponds to the variable name and value variable content. $ GLOBALS exists globally because $ GLOBALS is a super global variable. the following example shows how to use a super global variable:

Example 3. Examples of Super global variables and scopes

The instance code is as follows:

 

Use static variables

Another important feature of variable range is static variable ). static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. take a look at the following example:

Example 4. demonstrate the example that requires static variables

The instance code is as follows:

 

This function is useless, because every call will set the value of $ a to 0 and output "0 ". adding $ a ++ to the variable does not work, because $ a does not exist once you exit this function. to write a counting function that does not lose the current count value, you must define variable $ a as static:

Example 5. example of using static variables

The instance code is as follows:

Now, every time you call the Test () function, the $ a value is output and the value is added.

Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself. be careful when writing recursive functions, because infinite recursion may occur. make sure there are sufficient methods to abort recursion. the simple function calculates 10 recursively and uses the static variable $ count to determine when to stop:

Example 6. static variables and recursive functions

The instance code is as follows:

 

Note: static variables can be declared according to the preceding example. if you assign a value to the expression result in the declaration, parsing errors will occur.

Example 7. declare static variables

The instance code is as follows:

 

Reference of global and static variables

In the first generation of the Zend Engine, PHP4 is driven. the static and global definitions of variables are implemented in the form of references. for example, a real global variable imported using a global statement within a function domain is actually a reference to a global variable. this may lead to unexpected behaviors, as demonstrated in the following example:

The instance code is as follows:

 

Executing the preceding example will result in the following output:

Similar behaviors of NULLobject (stdClass) (0) {} are also applicable to static statements. References are not stored statically:

The instance code is as follows:

 Property ++; return $ obj;} function & get_instance_noref () {static $ obj; echo "Static object:"; var_dump ($ obj); if (! Isset ($ obj) {// assign an object to the static variable $ obj = new stdclass;} $ obj-> property ++; return $ obj ;} $ obj1 = get_instance_ref (); $ still_obj1 = get_instance_ref (); echo "\ n"; $ obj2 = response (); $ still_obj2 = get_instance_noref ();?>

Executing the preceding example will result in the following output:

Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) {  ["property"]=>  int(1)}

The previous example demonstrates that when a reference is assigned to a static variable, the value of the second call to the & get_instance_ref () function is not remembered.


Tutorial URL:

You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.

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.