1. Define constant define ("CONSTANT", "Hello world.");
Constants can only contain scalar data (boolean,integer,float and string).
When calling a constant, simply use the name to get the value of the constant, not the "$" symbol, 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 variable $a = "Hello";
3. Variable variable (use two dollar sign ($))
$ $a = "world";
All two variables are defined:
$a content is "hello" and $hello content is "world".
Therefore, it can be expressed as:
echo "$a ${$a}"; or echo "$a $hello"; they all output: Hello World
To use variable variables with arrays, you must resolve an ambiguous problem. This is when you write $ $a [1], the parser needs to know if you want to $a [1] as a variable, or if you want $ $a as a variable and take out the value indexed by [1] in the variable. The syntax for solving this problem is to use the ${$a [1]} for the first case, with ${$a}[1] for the second case.
4. Static variables
Static $a = 0 within the function;
Note: Assigning a value to an expression in a declaration can result in parsing errors such as static $a =3+3; (error)
A static variable exists only in a local function field (within a function), the value of the variable is not lost when the function is finished, and can be used to recursively call
5. Global Variables
Global variables defined in a function body can be used outside of the function body, and global variables defined in the function body cannot be used in the function body, and access to variables globally can be in a special PHP custom $GLOBALS array:
For example: $GLOBALS ["b"] = $GLOBALS ["a"] + $GLOBALS ["B"];
A real global variable imported in a function field with the global statement actually establishes a reference to a global variable
Global $obj;
Note: The static and global definitions of variables are implemented in an applied manner
6. Assign value to variable: Transfer address Assignment (simple reference):
$bar = & $foo; Add & sign to the variable that will be assigned the value
Changing the new variable will affect the original variable, and this assignment is faster
Note: Only named variables can pass address assignment
Note: if
$bar = & $a;
$bar = & $foo;
Changing the value of $bar can only change the value of variable foo without changing the value of a (reference changed)
7.PHP Super global variable $globals:
Contains a reference to a variable in the global scope that is valid for each current script. The key of the array is the name of the global variable. $GLOBALS array is present starting with PHP 3.
$_server: Variables are set by the WEB server or directly associated with the execution environment of the current script. Similar to the old array $HTTP _server_vars array (still valid, but opposed to use).
$_get: A variable that is submitted to a script via an HTTP get method.
$_post: A variable submitted to a script via the HTTP POST method.
$_cookie: A variable that is submitted to a script via an HTTP cookie method.
$_files: A variable submitted to a script via an HTTP POST file upload.
File Upload form should have enctype= "Multipart/form-data"
$_ENV: Executes the variables that the environment submits to the script.
$_request: A variable submitted to a script via the get,post and COOKIE mechanisms, so the array is not trustworthy. The existence of all variables contained in the array and the order of the variables are defined according to the Variables_order configuration instructions in php.ini. The array does not directly simulate an earlier version of the PHP 4.1.0. See Import_request_variables ().
Note: from PHP 4.3.0, the file information in $_files no longer exists in the $_request.
$_session: The variable currently registered to the script session.
How to disable Phpinfo ():
In php.ini
Disable_functions = Phpinfo ()
Restart the Web server.
Constants in PHP
Constants can only be used with define (constant name, constant value);
Constants can only contain scalar data (boolean,integer,float and string).
You can simply get the value of the constant by specifying its name, and do not precede the constant with the $ symbol. If the constant name is dynamic, you can also use a function
Constant () to read the value of the constant. You can get a list of all the defined constants with Get_defined_constants ().
Note: Constants and (global) variables are in different namespaces. This means that, for example, TRUE and $TRUE are different.
If you use an undefined constant, PHP assumes that you want the name of the constant itself, as if it were called with a string (CONSTANT corresponds to "CONSTANT"). A e_notice level error is emitted at this time. See the Manual for why $w 3sky[bar] is wrong (unless you define bar as a constant with define ()). If you only want to check whether a constant is defined, use the defined () function.
Constants and variables are different:
* Constants are not preceded by a dollar sign ($);
* Constants can only be defined with the Define () function, not through assignment statements;
* Constants can be defined and accessed in any place without regard to the rules of variable scope;
* Constants cannot be redefined or eliminated once defined;
* The value of a constant can only be a scalar.
Defining constants
<? Php
Define ("CONSTANT", "Hello world.");
Echo CONSTANT; Outputs "Hello world."
Echo Constant; Outputs "Constant" and issues a notice.
?>
An example of an automatic conversion variable type of PHP is the operator ' + ' of the addition. If any operand is a double-precision number, then all operands are evaluated as a double-precision number, and the result is also a double-precision number. Otherwise, the operand will be considered an integer, and the result will be an integer. Note that this does not affect the variable type of each operand itself, and the only change is how the operand is handled in the calculation process.
$foo = "0"; // $foo is a string , the value is "0" (ascii )
$foo ++; $foo is a string with a value of "1" (ascii )
$foo += 1; // $foo Now is an integer (2)
$foo = $foo + 1.3; // $foo is now a double-precision number (3.3) of the
$foo = 5 + "10 little piggies"; // $foo is an integer (15)
$foo = 5 + "10 small Pigs"; // $foo is an integer (1 5)
If you think the last two expressions in the above example look a little odd, see the "Convert String" section.
If you want to force a variable to be counted as a fixed type, see the "type coercion (casting)" section. If you want to change the type of a variable, see the description of the function "Settype ()".
Determine the type of a variable
Because PHP itself determines the type of variables and generally converts them as needed, the type of a particular variable is not obvious at any time. PHP includes a number of functions to find out the type of this variable. These functions are GetType (), Is_long (), is_double (), is_string (), Is_array (), and Is_object ().
Type coercion (casting)
Type coercion in PHP is likely to be similar to the C language: the type to be required is written in the parentheses in front of the stronger variable.
$foo = 10; $foo is an integer
$bar = (double) $foo; $bar is a double precision number
The following mandatory methods are allowed:
(int), (integer) – Force integer
(real), (double), (float) – Force double
(string) – Force as String
(array) – Force a group of
(object) – Force objects
Note that tab characters (tabs) and spaces (spaces) are allowed in parentheses, so the following statements are equivalent:
$foo = (int) $bar;
$foo = (int) $bar;
String conversion
When a string is computed as a numeric value, his result and type are determined as described below.
If the string contains the character '. ', ' e ', or ' e ', it is treated as a double-precision type variable, otherwise it is treated as an integer.
The value of this string is determined by the first part of the word. If this string starts with data of any valid number, the numeric data is the value of the string participating in the operation. Otherwise, the value is 0 (zero). Valid digital data is followed by the following tags, followed by one or more digits (can contain a decimal point), followed by an optional exponent. An exponent is made up of one or more numbers following the ' e ' or ' e '.
$foo = 1 + "10.5"; $foo is a double precision number (11.5)
$foo = 1 + " -1.3e3"; $foo is a double precision number (-1299)
$foo = 1 + "bob-1.3e3"; $foo is an integer (1)
$foo = 1 + "BOB3"; $foo is an integer (1)
$foo = 1 + "Small Pigs"; $foo is an integer (11)
$foo = 1 + "Little piggies"; $foo is an integer (11);
This string includes the character ' E '