Home  >  Article  >  Backend Development  >  photoshop cs5 official Chinese cracked version download PHP variable summary recommended for novices

photoshop cs5 official Chinese cracked version download PHP variable summary recommended for novices

WBOY
WBOYOriginal
2016-07-29 08:44:521227browse
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, you need to pass Reference assignment.
Reference Assignment
The created variable refers to the same content as 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
Variables can be declared anywhere in a PHP script, but the location where the variable is declared will greatly affect the scope of accessing the variable. This accessible range is called scope.
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. Exit When you declare a function on a variable, the variable and its corresponding value are deactivated. 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. Functions 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;
}
The function is executed and 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 ();
$somevar The displayed value 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 will be 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 (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 retains the previous value each time the function is executed. 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 amount of environment-related 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, the configuration parameter track_vars must be enabled in the PHP.INI file.

The above introduces photoshop cs5 official Chinese cracked version download PHP variable summary and recommended for novices, including photoshop cs5 official Chinese cracked version download content, I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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