Php variable Basic syntax

Source: Internet
Author: User
Tags drupal
Variables are very common in php. all operations must be passed through variables, next I will introduce some common usage of variables in php and some basic knowledge about php variables. the specific method is to store PHP variables in characters, numbers, arrays, and even objects... variables are very common in php. all operations must be passed through variables, next, I will introduce some common usage and basic knowledge about php variables in php,

Specific method:

PHP variables are used to store characters, numbers, arrays, and even object content, so that you can use. syntax to define a variable as needed:

$ Variable name = value;

Example of using variables:

The instance code is as follows:

 

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

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

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, let's use the following simple code:

The instance code is as follows:

 

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:

 

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 instance code is as follows:

// A. php file
 // B. php file
 

Why is the output 0 ?!!

In user-defined functions, a local function range will be 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 instance code is as follows:

 

// B. php file

 

2. excellent accessors

The instance code is as follows:

// A. php file

 

// B. php file

 

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. generally, executable code is stored, and constants may also be stored here. For example, the string constant BSS-Segment stores uninitialized variables. in a sense, non-initialized variables are garbage and Data-Segment is not available. global variables and static variables are stored here until the script is running, the operating system will reclaim the memory space 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 the memory is used up, they also need to manually release it.

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 have little significance. static variables declared inside the function are limited by the scope, and the internal static variables 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 instance code is as follows:

 

$ I exists both inside 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 a global variable. when the global_var () function is called, no memory space is allocated to $ j. 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. after exiting the function, it is destroyed.

The instance code is as follows:

 

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, but 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 variables will not be recycled, but will not be modified by other functions. (Note: The $ c outside the function and in the function are two different variables)

The instance code is as follows:

 

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 if they are not specified.

Reference functions and static variables

Since static variables will not be destroyed until the execution of the script ends, is there any way to access the value of this variable? Let's take a look at the following example:

The instance code is as follows:

 

The output values of the two question marks are 8 and 12, respectively, indicating that the variables can still be accessed as long as they are not destroyed. we can return the address of the static variable to other functions by referencing the function. other functions can access and modify its value through the address of the static variable.

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 assignment, resetting, and deletion. I think this is a good way.

The instance code is as follows:

 $ 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 (! Emptyempty ($ tags) & in_array ($ ip_address, $ tags, 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 (); // There are many such application methods in drupal, convert the Stas of other functions Tic variables are stored in one place, that is, drupal_static data, and then managed in a unified manner, such as resetting.?>


Tutorial address:

Reprinted! But please include 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.