PHP USES summaries for learning variables

  • 2020-03-31 21:39:23
  • OfStack

1. Define constants Define (" CONSTANT ", "Hello world");
Constants can only contain scalar data (Boolean, integer, float, and string).
When calling a CONSTANT, you simply need to get the value of the CONSTANT by name, instead of adding the "$" sign, such as: echo CONSTANT;
Note: constants and (global) variables are in different namespaces. This means that for example TRUE and $TRUE are different.

2. Ordinary variables $a = "hello".

3. Variable variables (using two dollar signs ($))
$$a = "world";
Both variables are defined:
The content of $a is "hello" and the content of $hello is "world".
Therefore, it can be expressed as:
Echo "$a" ${$a}; Or   Echo $a $" hello "; They all output: hello world
To use mutable variables in arrays, you must resolve an ambiguity. This is why when you write $$a[1], the parser needs to know whether it wants $a[1] as a variable or $$a as a variable and fetch the value indexed [1] in that variable. The syntax to solve this problem is to use ${$a[1]} for the first case and ${$a}[1] for the second case.

4. Static variables
Inside the function static $a = 0;
Note: assigning an expression to it with the result of the statement results in a parsing error such as static ; $a = 3 + 3; (error)
Static variables exist only in the local function domain (inside the function), and after the function is executed, the value of the variable is not lost and can be used for recursive calls

5. Global variables
Global variables defined in the body of the function can be used outside the body of the function. Global variables defined in the body of the function cannot be used inside the body of the function. Variables accessed in the global scope can be customized with a special PHP $GLOBALS array:
For example: $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
A true global variable imported in a function field with a global statement actually creates a reference to the global variable
Global $obj;
Note: the static and global definitions of variables are implemented as applications

6. Assign a value to a variable: assign an address (simple reference) :
$bar = & $foo;   // + & to the variable to be assigned
Changing the new variable affects the original variable, which is a faster assignment
Note: only named variables can be assigned to addresses
Note: if
$bar = & $a;
$bar = & $foo;
Changing the value of $bar can only change the value of the variable foo, not the value of a (the reference changed)

7.PHP super global variables $GLOBALS: contains a reference to a valid variable within the global scope of each current script. The key of this array is marked with the name of the global variable. The $GLOBALS array has existed since PHP 3.
$_SERVER: the variable is set by the Web server or is directly associated with the execution environment of the current script. Similar to the old array $HTTP_SERVER_VARS array (still valid, but not used).
$_GET:       A variable submitted to a script via the HTTP GET method.
$_POST,     A variable submitted to the script via the HTTP POST method.
$_COOKIE: the variable submitted to the script via the HTTP cookie method.
$_FILES:   A variable submitted to a script via an HTTP POST file upload.
Enctype ="multipart/form-data"
$_ENV:       Variables that the execution environment commits to the script.
$_REQUEST: a variable submitted to a script via the GET, POST, and COOKIE mechanisms, so the array is not trusted. The presence or absence of all variables contained in the array and the order of the variables are defined according to the variables_order configuration directive in php.ini. This array does not directly emulate earlier versions of PHP 4.1.0. See import_request_variables ().
Note:           Since PHP 4.3.0, the file information in $_FILES no longer exists in $_REQUEST.
$_SESSION: the variable currently registered to the script session.

Related articles: