Php variable Basic syntax

Source: Internet
Author: User
Tags php file php and php and mysql php script reset drupal

Method

PHP variables

PHP variables are used to store characters, numbers, arrays, and even object content for use where we need them.

Syntax for defining a variable:

$ Variable name = value;

Example of using variables:

 

The code is as follows: Copy code

<? Php

$ Var_char = "Hello! ";

Echo $ var_char;

?>

The output result of executing this code is:


1 Hello!
From this example, we can see that the variable name starts with $, indicating that this is a variable. Variable names start with letters (a-z, A-Z) or underscores (_), followed by any letter or number and underline, but not a space.

The following variable names are valid:


$ Var_char

$ VarChar

$ _ VarChar

$ Var_char5

Prompt

For variable naming, we recommend that you use a variable name to indicate a combination of actual semantics, for example, $ my_name or $ myName.


Global


Global variables are one of them. Of course this is quite controversial. Many people suggest disabling global variables. Improper use may cause poor program readability! The structure is messy and debugging is even more confusing, but it is undeniable that it is convenient. This is why php uses the global variable !...

Today we have encountered the problem that php global variables do not work.

First, a simple code:

The code is as follows: Copy code

<? Php
$ A = 0;
Function Test ()
{
$ A = 1;
}
Test ();
Echo $;
?>

The output in the above code is 0, because the $ a variable in the Test function is set as a local variable by default, and the $ a scope is in the Test. Modify the code as follows:

The code is as follows: Copy code

<? Php
$ A = 0;
Function Test ()
{
Global $ a; // declare that the $ a variable used in the Test function is a global variable.
$ A = 1;
}
Test ();
Echo $;
?>

After the $ a variable used in the Test function is declared as a global variable, the output of $ a is 1.
The above example is just a basic knowledge of global variables. Let's look at the complex points:

The code is as follows: Copy code

// A. php file

<? Php
Function Test_Global ()
{
Include 'B. Php ';
Test ();
}

$ A = 0;
Test_Global ();
Echo $;
?>

// B. php file

<? Php
Function Test ()
{
Global $ a; // declare that the $ a variable used in the Sum function is a global variable.
$ A = 1;
}
?>


Why is the output 0 ?!!

In a user-defined function, a local function range is introduced. Any variables used in the function will be limited to the local function scope by default (including the variables in the include and require imported files )!
Explanation:. test_Global in the PHP file is a defined third-party function, which is imported into B using include. the global variable of $ a in the PHP file, so $ a is restricted to the range of local functions of Test_Global, so B. $ a in the PHP file takes effect in Test_Global, instead of. php ....

Solution:

1. Rush out of the local function // A. php file

The code is as follows: Copy code

<? Php
Function Test_Global ()
{
Test ();
}
Include 'B. Php'; // Remove include from the local Test_Global function
$ A = 0;
Test_Global ();
Echo $;
?>

// B. php file

<? Php
Function Test ()
{
Global $;
$ A = 1;
}
?>


2. Excellent accessors

The code is as follows: Copy code

// A. php file
<? Php
Include 'B. Php ';
$ A = 0;
Set_Global ($ );
Echo $;
?>

// B. php file

<? Php
Function Set_Global (& $ var)
{
$ Var = 1;
}
?>


 

Php variable reference function static variable

"No matter how variables are declared (by value or by reference), variables can be declared anywhere in the PHP script. However, the declared location will greatly affect the scope of the access variable. This accessible scope is called scope "-PHP and MySQL programming
Before learning about php variables, let's take a look at the Division of memory segments in the segmented memory management architecture. Generally, the operating system divides the physical memory into the following logical segments.


Text-Segment, the biggest feature of this Segment is read-only. It generally stores executable code, or constants, such as string constants.
BSS-Segment, which stores uninitialized variables. In a sense, variables without initialization are junk and unavailable.
Data-Segment: global variables and static variables are stored here. The operating system will not recycle the memory space until the script stops running, and the variables will be destroyed.
The parameters of the Stack-Heap Segment function and local variables (also called local variables) are stored in the Heap (stack) and return values. When it is used up, the operating system will recycle this part of memory space. For C programmers, they can manually apply for memory space from the stack (heap). Once they are used up, they also need to manually release the memory.
As a PHP program, we are concerned with global variables, static variables, local variables, function parameters, and function return values. The local variables are basically the same as the function parameters. The memory space is allocated during initialization. After exiting the function, the operating system recycles the memory space. The global variables and static variables are released only after the php script is run. Unlike global variables, all static variables are initialized and allocated memory space before the program is executed.

Note:
1. Static variables declared outside the function are of little significance. The static variables declared inside the function are limited by the scope. The static variables inside the function cannot be modified outside the function.
2. The referenced variable is also a variable, but its value is the memory address of the variable.

Php reserved words global and static

The code is as follows: Copy code
<? Php
$ I = $ j = 8;
Function global_var (){
Global $ j, $ B;
$ C = $ j = $ B = $ I = 4;
}
Global_var ();
Echo "I: $ I, j: $ j B: $ B c: $ c n"; // I: 8, j: 4 B: 4 c:
?>

$ I is included in and outside the function, but the two of them are completely different variables. $ I outside the function is a global variable, and the memory space will not be released until the script stops running. $ I in the function is a local variable. When the program stream passes through the function, it is initialized. When the function is exited, the memory is recycled by the system and the function is called again, the memory space is allocated again and the memory space is recycled. The allocated memory space may be the same memory address or the same memory address.
Unlike $ I, $ j converts the keyword global to "global variable. When the global_var () function is called, the memory space of $ j is not allocated again. Similarly, $ B can be printed out of the function, but $ c cannot be printed because $ B is a global variable and will not be destroyed. $ C cannot be printed, and $ c does not exist anymore. After exiting the function, it is destroyed.

The code is as follows: Copy code
<? Php
$ A = 2;
Static $ B = 2;
Static $ c = 3;
Function global_var (){
Static $ I, $ c;
Global $ j, $;
$ C = $ a = $ B = $ I = $ j = 4;
}
Global_var ();
Echo "a: $ a B: $ B c: $ c I: $ I j: $ j"; // a: 4 B: 2 c: 3 I: j: 4
?>

First, we can see $ B and $ c outside the function, that is, global variables and static variables. Static modification does not make much sense here, because they are all stored in the data segment (data-segment) and will not be recycled until the script is run out. Then, let's look at $ I and $ c in the function. After the function is called, $ I and $ c are not actually recycled, however, the $ I output is NULL and the $ c output is 3, because their scope is inside the function, not outside the function, that is, $ I and $ c are invisible outside the function. The significance of the static variable in a function lies in this: it is visible only inside the function and will not be destroyed. That is to say, the warranty letter exits the function, and the variable will not be recycled, but will not be modified by other functions. (Note: The $ c variable outside the function and in the function is two different variables)

The code is as follows: Copy code
<? Php
Function global_var (){
Static $ I;
+ + $ J;
++ $ I;
Echo "j: $ j I: $ I n ";
}
Global_var (); // j: 1 I: 1
Global_var (); // j: 1 I: 2
Global_var (); // j: 1 I: 3
?>

In the preceding example, $ j is always 1, and $ I accumulates 1 every time it is called. This is because local variables are stored in the heap segment and will be recycled every time you exit the function. $ I is stored in the data segment (data-segment) and will not be recycled until the program is executed. Static variables we usually call are static variables in the function unless otherwise specified.

Reference functions and static variables
Static variables will not be destroyed until the execution of the script ends. Is there a way to access the value of this variable? Let's take a look at the following example:

The code is as follows: Copy code
<? Php
Get_local_static_var ();
$ Ptr = & get_local_static_var ();
Get_local_static_var ();
+ + $ Ptr;
Get_local_static_var ();
What_ I ($ ptr );
Get_local_static_var ();//??
What_p ($ ptr );
Get_local_static_var ();//??
 
Function & get_local_static_var (){
Static $ I;
++ $ I;
Echo "I: $ I n ";
Return $ I;
}
 
Function what_ I ($ ptr ){
$ I = & get_local_static_var ();
++ $ I;
+ + $ Ptr;
}
 
Function what_p (& $ ptr ){
$ I = & get_local_static_var ();
++ $ I;
+ + $ Ptr;
}
?>

The two question marks are output at 8 and 12 respectively. This shows that variables can still be accessed as long as they are not destroyed. We can return the static variable address to other functions by referencing the function. Other functions can access and modify the value of static variables.
First in the above example ??, Why 8 instead of 9. This is because the what_ I ($ ptr) function requires that the parameter be passed by value, that is, the $ ptr real parameter value here is 5, the $ ptr parameter and the global variable $ ptr are two different variables. Second place ?? The value is 12. Why is it not 11. What_p (& $ ptr) function, requires that the parameter is passed by reference, that is, $ ptr here is the address pointing to the static variable $ I, note that the $ ptr parameter and the global variable $ ptr are two different variables, but they all point to the same place.

Drupal application appreciation
Drupal defines a drupal_static function. The static variables of other functions are stored in an array, and all static variables applied to drupal are managed in a unified manner, such as assigning values, resetting, and deleting. I think this is a good way.

The code is as follows: Copy code

<?
Function & drupal_static ($ name, $ default_value = NULL, $ reset = FALSE ){
Static $ data = array (), $ default = array ();
If (! Isset ($ name )){
Foreach ($ default as $ name => $ value ){
$ Data [$ name] = $ value;
    }
Return $ data;
  }
If ($ reset ){
If (array_key_exists ($ name, $ default )){
$ Data [$ name] = $ default [$ name];
    }
Else {
Return $ data;
    }
  }
Elseif (! Array_key_exists ($ name, $ data )){
$ Default [$ name] = $ data [$ name] = $ default_value;
  }
Return $ data [$ name];
}
 
Function ip_address (){
$ Ip_address = & drupal_static (_ FUNCTION __);
 
If (! Isset ($ ip_address )){
$ Ip_address = $ _ SERVER ['remote _ ADDR '];
 
If (variable_get ('reverse _ proxy', 0 )){
If (array_key_exists ('http _ X_FORWARDED_FOR ', $ _ SERVER )){
$ Reverse_proxy_addresses = variable_get ('reverse _ proxy_addresses ', array ());
If (! Empty ($ reverse_proxy_addresses )&&
In_array ($ ip_address, $ reverse_proxy_addresses, TRUE)
){
$ Ip_address_parts = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']);
$ Ip_address = trim (array_pop ($ ip_address_parts ));
        }
      }
If (array_key_exists ('http _ X_CLUSTER_CLIENT_IP ', $ _ SERVER )){
$ Ip_address = $ _ SERVER ['http _ X_CLUSTER_CLIENT_IP '];
      }
    }
  }
 
Return $ ip_address;
}
 
Ip_address ();
// In drupal, many such applications store the static variables of other functions in one place, that is, drupal_static data, and then perform unified management, such as resetting.
?>

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.