Home > Backend Development > PHP Tutorial > Summary of PHP variables Recommended for beginners_PHP tutorial

Summary of PHP variables Recommended for beginners_PHP tutorial

WBOY
Release: 2016-07-21 15:30:43
Original
717 people have browsed it

There is no need to explicitly declare variables in PHP. Variable declaration can be done at the same time as assignment. Good programming practice is: all variables should be declared before use, preferably with comments.

1. Assignment of variables

After a variable is declared, it can be assigned a value. There are two ways: value assignment and reference assignment.
1. Value assignment
$color = "red" ;
$sum = 12+"15" ; /* $sum = 27 */

2. If you want two variables To point to the same copy of a value, assignment by reference is required.
Reference assignment
The variable created has the same content as that referenced by another variable. If multiple variables refer to the same content, modifying any one of them will be reflected in the remaining variables.
Example:
$value1 = "hello" ;
$value2 = &value1 ; /*$value1 and $value2 both equal "hello" .*/
$value2 = "goodbye" ; /*$value1 and $value2 both equal "goodbye". */
?>

2. Scope of variables
PHP script Variables can be declared anywhere, but where a variable is declared greatly affects the scope of access to the variable. This accessible range is called scope.
The 4 scopes of PHP variables:
△ Local variables
△ Function parameters
△ Global variables
△ Static variables
1. Local variables
Variables declared in a function are considered local variables and can only be referenced in the function. When exiting the function in which the variable is declared, the variable and the corresponding value will be deleted. Eliminates the possibility of intentionally or unintentionally modifying a globally accessible variable.

$x = 4 ;
function assignx ( ) {
$x = 0 ;
print "$x inside function is $x .
" ;
}
assignx ( ) ;
print "$x outside of function is $x .
" ;
The execution result of the code is:
$x inside function is 0 .
$ x outside function is 4.

2. Function parameters
PHP is the same as other programming languages. Any function that accepts parameters must declare these parameters in the function header. Although these parameters accept values ​​from outside the function, they are no longer accessible after exiting the function. (Except parameters passed by reference)
For example:
function x10 ( $value ) {
$value = $value * 10 ;
return $value ;
}
Function execution Completed, the parameters will be revoked.

3. Global variables
Contrary to local variables, global variables can be accessed anywhere in the program. When changing a global variable in a function, you need to explicitly declare the variable as a global variable in the function. Just add GLOBAL before the variable in the function.
For example:
$somevar = 15;
function addit ( ) {
GLOBAL $somevar;
$somevar ++;
print "somevar is $somevar";
}
addit ( ) ;
The displayed value of $somevar should be 16. However, if the GLOBAL $somevar ; line is removed, the variable $somevar will be implicitly set to 0, plus 1, and the final displayed value The value is 1.
Another way to declare global variables is to use PHP's $GLOBAL array, as follows:
$somevar = 15;
function addit () {
$GLOBALS[ 'somevar' ]++;
}
addit ( ) ;
print "somevar is ". $GLOBALS[ 'somevar' ] ;
The return value is as follows: somevar is 16 .

4. Static Variable
static scope. The function parameters of ordinary variables will be destroyed when the function ends, but static variables will not lose their value when the function exits, and they can retain this value when the function is called again. You can declare a static variable by adding the keyword STATIC before Bianliangming.
STATIC $somevar ;
Consider an example:
function keep_track ( ) {
STATIC $count = 0 ;
$count ++ ;
print $count ;
print "
" ;
}
keep_track ( ) ;
keep_track ( ) ;
keep_track ( ) ;
keep_track ( ) ;
If $count is not specified as static (Correspondingly, $count is a local variable), the output will be
1
1
1
1
Because $count is static, it will be executed every time the function is executed. Keep the previous value. The output is as follows:
1
2
3
4
Static scope is useful for recursive functions. A recursive function is a powerful programming concept. It is a function that calls itself repeatedly until a certain condition is met.

5. PHP’s super global variables
PHP provides many useful predefined variables, which can be accessed by the person and location executing the script, and are used to provide a large number of environment-related variables. information. You can use these variables to obtain detailed information about the current user session, the user's operating system environment, and the local operating environment. PHP creates some variables, while the availability and value of many other variables depends on the operating system and WEB service.

Output all predefined variables:
foreach ( $_SERVER as $var => $value ) {
echo "$var => $value
" ;
}
Display the user's IP address:
print "HI!Your IP address is ".$_SERVER[ ' REMOTE_ADDR' ] ;

To use the predefined variable array in PHP, it must be in PHP. Enable the configuration parameter track_vars in the INI file.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323133.htmlTechArticleThere is no need to explicitly declare variables in PHP. Variable declaration can be done at the same time as assignment. Good programming practice is: all variables should be declared before use, preferably with comments. 1. Variables...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template