- $a = ' Hello ';
- function Test () {
- Var_dump ($a);
- }
- Test ();
- Include ' B.inc ';
Copy Code
B.inc content:
- echo ' Hello ';
- ?>
Copy Code
The program can output hello normally, but the annotated test () cannot parse correctly because the variable $ A is undefined.
4. Use global variables if you want to use global variables in a function, there are two ways to do it.
Global keyword Global $a, $b; After you declare the globals in a function, all references to any of the variables point to their global version.
$GLOBALS Super global variable array $globals[' B '] = $GLOBALS [' a '] + $BLOBALS [' B ']; usage is similar to the Global keyword.
5 static variable static variable exists only in local function domain, but its value is not lost when program execution leaves this 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.
- function Test () {
- static $cnt = 0;
- Echo $cnt;
- $cnt + +;
- if ($cnt < 10) {
- Test ();
- }
- $CNT--;
- }
- Test ();
Copy Code
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 < 10) {
- Test ();
- }
- cnt--;
- }
- int main (void) {
- Test ();
- return 0;
- }
Copy Code
The static and global definitions for variables are implemented in a reference manner.
5, variable variable variables are a special use of PHP language, I do not know the existence of other languages.
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.
- $a = ' Hello ';
- $ $a = ' world ';
- echo "$a $ $a"; Hello $hello
- echo "$a ${$a}"; Hello World
Copy Code
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 takes out the value of index 1 in the variable.
6. 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.
Copy Code
Form processing file process.php.
- Var_dump (Isset ($_post[' my.text '));
- Var_dump (Isset ($_post[' mytext '));
- Var_dump (Isset ($_post[' my_text '));
- Var_dump ($_post[' my_text ');
Copy Code
Because the period is not a valid character in the PHP variable name, the output: Boolean Falseboolean Falseboolean True
The string ' H3 ' (length=2) MAGIC_QUOTES_GPC configuration directive affects the value of Get/post/cooie, which is deprecated and is not escaped by 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 understands arrays in the context of form variables.
example, use more complex form variables and post the form to yourself and display the data at commit time.
- if (Isset ($_post[' action ')) {
- Var_dump ($_post);
- } else {
- $page = $_server[' php_self ');
- $s = <<<>
- STR;
- Echo $s;
- }
Copy Code
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.
- if (Isset ($_post[' action ')) {
- Var_dump ($_post);
- } else {
- $s = <<<>
- STR;
- Echo $s;
- }
Copy Code
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 ' = = String ' 282 ' (Leng th=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.
PHP Cookies use:
Cookie data is available in the corresponding cookie array, and if multiple values are assigned to a cookie variable, they must be assigned to a group.
|