PHP's Variable summary novice recommended _php Foundation

Source: Internet
Author: User
you do not need to display the declaration variable in PHP, and the variable declaration can be performed concurrently with the assignment. Good programming habits: all variables should be declared before use, preferably with annotations.

the assignment of a variable

Variable declaration 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 assign a value by reference.
Reference assignment
The variable you create is the same as the one referenced by the other variable, and if multiple variables refer to the same content, modifying any one of them will be reflected on the rest of the variables.
Example:
<?php
$value 1 = "Hello";
$value 2 = &value1; /* $value 1 and $value 2 both equal "Hello". * *
$value 2 = "Goodbye"; /* $value 1 and $value 2 both equeal "Goodbye". */
?>

ii. Scope of the variable
A variable can be declared anywhere in a PHP script, but the location of the declared variable can greatly affect the scope of the access variable. This accessible range is called scope (scope).
4 Scope of PHP variables:
Local variables
function arguments
Global variables
static variables
1. Local variables
A variable declared in a function is considered a local variable and can only be referenced in a function, and the variable and the corresponding value are revoked when the function that declares the variable is exited. Eliminates the possibility that variables that cause global access are intentionally or unintentionally modified.

$x = 4;
function Assignx () {
$x = 0;
Print \ $x inside function is $x. <br> ";
}
Assignx ();
Print \ $x Outside of function is $x. <br> ";
The execution result of the code is:
$x inside function is 0.
$x outside function is 4.

2, function parameters
PHP and other programming languages, any function that accepts parameters must declare these parameters in the first part of the letter. Although these parameters accept values outside the function, they are no longer accessible after exiting the function. (except for parameters passed by reference)
For example:
function x10 ($value) {
$value = $value * 10;
return $value;
}
function completed, parameter is about to be undone.

3. Global variables
In contrast to local variables, global variables can be accessed at any location in the program. When you change a global variable in a function, you need to display the variable in the function as a global variable, as long as you add global to the variable in the function.
For example:
$somevar = 15;
function Addit () {
GLOBAL $somevar;
$somevar + +;
Print "Somevar is $somevar";
}
Addit ();
The value $somevar display should be 16, but if the GLOBAL $somevar is removed; This line, the variable $somevar will be implicitly set to 0, plus 1, the last value shown is 1.
Another way to declare a global variable is to use the $global array of PHP, 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 arguments of a normal variable are undone at the end of the function, but the static variable does not lose value when the function exits, and it retains the value when the function is called again. You can declare a static variable by adding the keyword static before Sheng Liangming.
STATIC $somevar;
Consider an example:
function Keep_track () {
STATIC $count = 0;
$count + +;
Print $count;
print "<br>";
}
Keep_track ();
Keep_track ();
Keep_track ();
Keep_track ();
If the $count is not indicated as static (the corresponding $count is a local variable), the output will be
1
1
1
1
Because the $count is static, it retains the previous value each time the function is executed. The output is as follows:
1
2
3
4
Static scopes are useful for recursive functions. Recursive functions (recursive function) is a powerful programming concept, which is a function that can call itself repeatedly until a certain condition is met.

5, PHP's Super global variables
PHP provides a number of useful predefined variables that can be accessed at the person and location where the script is executed to provide a wealth of information about the environment. These variables provide detailed information about the current user session, the user 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 services.

Output all predefined variables:
foreach ($_server as $var => $value) {
echo "$var => $value <br>";
}
To display the user's IP address:
Print "hi! Your IP address is ". $_server[' REMOTE_ADDR '];

To use predefined variable arrays in PHP, you must enable the configuration parameter Track_vars in the php.ini file.

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.