: This article mainly introduces the variable-PHP manual notes. if you are interested in the PHP Tutorial, refer to it. Basic
Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case sensitive and Chinese characters may also be valid.
By default, values are always assigned. PHP also provides another way to assign values to variables: assign values to references. This means that the new variable simply references the original variable (in other words, "become its alias" or "point. Changing new variables will affect the original variables, and vice versa. Assign values by reference. simply add an & symbol to the variable to be assigned (source variable ). Note: only variables with names can reference values.
Predefined variables
PHP has a large number of predefined variables, and many variables depend on the server. some predefined variables do not take effect when running in the form of command lines.
PHP provides an additional set of pre-defined arrays. These array variables contain data from the web server (if available), runtime environment, and user input, which is usually called autoglobals) or superglobals ).
Variable range
The variable range here refers to the context defined by it, that is, its effective range. The scope of the variable contains the files introduced by include and require.
The global variables in PHP are a little different from those in C. in C, global variables automatically take effect in functions unless they are overwritten by local variables. Global variables in PHP must be declared as global when used in functions.
b.inc
The content is as follows:
This program can be output normallyhello
But commented outtest()
The parsing fails because the variable $ a is undefined.
Use global variables
If you want to use global variables in a function, you can use the following two methods.
Global keyword
global $a, $b;
After a global variable is declared in a function, all references to any variable will point to its global version.
$ GLOBALS ultra-global variable array
$GLOBALS['b'] = $GLOBALS['a'] + $BLOBALS['b'];
The usage is similar to that of the global keyword.
Static variables
Static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. In addition, it is initialized only once during Declaration. The value of a static function is not overwritten when a function is called each time.
If a value is assigned to a static variable in the declaration result, the parsing error is caused. the static declaration is parsed during compilation.
Similar to the C language static, the following C code can also be set to 0 ~ Output 10 numbers in sequence.
#include void test(void) {static int cnt = 0;printf("%d ", cnt);cnt++;if(cnt < 10) {test();}cnt--;}int main(void) {test();return 0;}
Static and global definitions of variables are implemented by reference.
Variable
Variable variables are a special usage in the PHP language and do not exist in other languages.
In short, a variable is a variable that obtains the value of a common variable as the variable name of the variable.
When variables are used in arrays, ambiguity may occur. For example, write$$a[1]
The compiler will report an error. you need to replace the meaning in the following two ways.
${$a[1]}
$ A [1] as a variable
${$a}[1]
$ A acts as a variable and retrieves 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.$_GET[]
,$_POST[]
And$_REQUEST[]
Access.
Note: the points and spaces in the variable name are converted into underscores. For example
Changed$_REQUEST["a_b"]
The following example shows the use of identifiers in the form.
Form processing fileprocess.php
.
Because the period is not a valid character in the PHP variable name, the output result is as follows.
boolean falseboolean falseboolean truestring 'h3' (length=2)
magic_quotes_gpc
The configuration instruction affects the value of get/post/cooie. this feature has been removed and the single quotation marks, double quotation marks, backslash, and NULL characters in the input are not escaped. You can useaddslashes()
To reference a referenced string, usestripslashes()
.
PHP also understands arrays in the context of Form variables. the following example uses more complex form variables, post the form to itself, and display data when submitting the form.
Be careful when heredoc contains Complex Variables. the above code$_SERVER['PHP_SELF']
If no curly braces are added, an error is reported during running.
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 submitting a form, you can use an image instead of the standard submit button. It's amazing to use it for the first time.
For the above program, when the user clicks somewhere in the image, the form will be transmitted to the server with two variables addedsub_x
Andsub_y
Contains the coordinates of the image you click.
array (size=3) 'action' => string '1' (length=1) 'sub_x' => string '334' (length=3) 'sub_y' => string '282' (length=3)
cookies
PHP can be usedsetcookie()
The function sets cookies. cookies are part of the HTTP header and must be called before sending any output to the browser.
The usage of cookies is as follows.
Cookie data is available in the corresponding cookie array. if you assign multiple values to a cookie variable, you must assign it to an array.
(Full text)
The above describes the variable-PHP manual notes, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.