Variable base
Variables in PHP are represented by a dollar sign followed by a variable name. Variable names are case-sensitive, and it may be legal to appear in Chinese.
Variables are always assigned values by default. PHP also provides another way to assign a value to a variable: reference assignment. This means that the new variable is a simple reference (in other words, "becomes its alias" or "points to") the original variable. Changing the new variable will affect the original variable and vice versa. Using a reference assignment, simply add a & symbol to the variable that will be assigned (the source variable). Note that only a variable with a name can reference the assignment.
Pre-defined variables
PHP has a large number of predefined variables, many of which are dependent on the server, and some of the predefined variables run as command lines without taking effect.
PHP provides an additional set of predefined arrays that contain data from the Web server (if available), the runtime environment, and user input, often referred to as automatic global variables (autoglobals) or Hyper global variables (superglobals).
Variable Range
The variable scope here refers to the context in which it is defined, that is, its scope of entry. The scope of the variable contains the files introduced by include and require.
PHP has a slightly different global variable from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. Global variables in PHP must be declared globally when used in functions.
B.inc content is as follows:
The program can output hello normally, but the annotated test () cannot parse correctly because the variable $ A is undefined.
Using Global variables
If you want to use global variables in a function, there are two ways to do it.
Global keyword
Global $a, $b;
When a global variable is declared in a function, all references to either variable point to its global version.
$GLOBALS an array of hyper-global variables
$GLOBALS [' b '] = $GLOBALS [' a '] + $BLOBALS [' B '];
Usage is similar to the Global keyword.
static variables
A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope. Also, only one time is initialized at the time of declaration, and each call to the function does not overwrite the value of the static function.
A static variable is assigned a value in the declaration with the result of an expression, resulting in a parse error, which is parsed at compile time.
Similar to the static C language, the following C code can also be 0~9 10 numbers in turn output.
#include
void Test (void) { static int cnt = 0; printf ("%d", CNT); cnt++; if (CNT <) { test (); } cnt--;} int main (void) { test (); return 0;}
The static and global definitions for variables are implemented in a reference manner.
Variable variable
mutable variables are a special use in the PHP language and do not know that other languages exist.
In short, a mutable variable is a variable variable that gets the value of an ordinary variable as the variable name of the variable variable.
Ambiguous problems can occur when mutable variables are used in arrays. For example, write the $ $a [1], the compiler will error, you want to express the meaning needs to be replaced in the following two ways.
${$a [1]}
$a [1] as a variable
${$a}[1]
$ $a as a variable and take out the value of index 1 in the variable.
form variables
When a form is submitted to a PHP script, the information in the form is automatically available in the script and can be accessed through $_get[], $_post[], and $_request[].
Note that the points and spaces in the variable name are converted to underscores. For examplebecomes $_request["A_b", the following example shows the use of identifiers in a form.
Form processing file process.php.
Because the period is not a valid character in the PHP variable name, the output is as follows.
Boolean Falseboolean falseboolean truestring ' h3 ' (length=2)
The MAGIC_QUOTES_GPC configuration directive affects the value of Get/post/cooie, which has been deprecated and is not escaped from single quotes, double quotes, backslashes, and null characters in the input. If you need to escape using addslashes (), you need to use stripslashes () if you need to dereference a referenced string.
PHP also knows the array in the context of a form variable, and the following example uses more complex form variables and posts the form to itself and displays the data when it is submitted.
Take extra care when you have complex variables in Heredoc, and the above code $_server[' php_self ' does not add curly braces to the run times error.
Array (size=4) ' personal ' = = Array (size=2) ' name ' = = String ' Hello ' (length=5) ' detail ' = String ' World ' (length=5) ' option ' = = Array (size=2) 0 = = String ' A ' (length=1) 1 = String ' C ' ( length=1) ' action ' + string ' submitted ' (length=9) ' submit ' + string ' Submit query content ' (length=12)
Image submission
When you submit a form, you can use a picture instead of the standard submit button. First time use, really good magic.
For the above program, when the user clicks on the image somewhere, the form will be transferred to the server, and add two variables sub_x and sub_y, which contains the coordinates of the user click the picture.
Array (size=3) ' action ' = = String ' 1 ' (length=1) ' sub_x ' = = String ' 334 ' (length=3) ' sub_y ' = Strin G ' 282 ' (length=3)
Cookies
PHP can use the Setcookie () function to set Cookies,cookies as part of the HTTP information header, so it must be called before any output is sent to the browser.
The related use of cookies is shown below.
!--? php if (isset ($_cookie[' cnt])) {$cnt = $_cookie[' cnt '] + 1; echo $cnt;} else {$cnt = 1;} Setcookie (' CNT ', $cnt, time () + 3600); The Cookie data is available in the appropriate cookie array, and if multiple values are assigned to a cookie variable, it must be assigned to a group. The
constant syntax Constant does not change its value during script execution. Constant large lowercase sensitive, traditionally the constant identifier is always uppercase. Once a constant is defined, it cannot be redefined or undefined, and the value of a constant can only be scalar.
Can use the Define () function to define constants, or you can use the Const keyword to define constants outside of the class definition. Custom constants do not start with double underscores and may conflict with magic constants.
define (' FOO ', ' something '); const FOO = ' something '; The Use the Const keyword to define constants that must be at the top of the scope, because this method is defined at compile time. Constants can be accessed from anywhere in the script, regardless of the scope of the constant, which is global. The
Magic constant Magic constant value can change, but it is not a constant.
__line__//The current line number in the file __file__//The full path and file name of the file, absolute path __dir__//directory where the file is located, unless the root directory, otherwise excluding the trailing slash, equivalent to DirName (__file__) __function__//function name, case-sensitive __class__//class name, including its declared action area __trait__//TRAIT name, including its declared function area __method__//class method Name _ _namespace__//Name of the current namespace
Any script that PHP runs to it provides a number of predefined constants, and the values of the above 8 magic constants change as they change position in the code.
(end of full text)