Php constants and variable usage. In the past, I rarely described the usage and reference tables of variables, constants, and magic constants in php in such detail, this article is of great help to beginners. if you need to know more about the usage of variables, constants, and magic constants in php and the reference table, this article is of great help to beginners. For more information, see.
Variable:
Variables are used to store values, such as numbers, text strings, or arrays.
Once a variable is set, we can reuse it in the script.
All variables in PHP start with the $ symbol.
The correct method for setting variables in PHP is:
The code is as follows: |
|
$ Var_name = value; |
PHP beginners often forget the $ symbol before the variable. In this case, the variable will be invalid.
Let's try to create a variable with a string and a variable with a numeric value:
The code is as follows: |
|
$ Txt = "Hello World! "; $ Number = 16; ?>
|
1. how to define a variable? what is the difference between it and C?
Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case sensitive. For example:
The code is as follows: |
|
$ Var = 'Jim '; $ VAR = 'Kimi; Echo "$ var, $ VAR"; // output "Jim, Kimi"
|
?> You may also care about variable naming, which is actually the same as most languages.
2. are variables case sensitive?
As stated in 1, it is case sensitive.
Note: Since PHP4, the concept of reference assignment has been introduced, which is similar to that of most languages. However, I think C/C ++ is the most similar. because it also uses the "&" symbol. For example:
The code is as follows: |
|
1 2 $ foo = 'Bob'; // assign 'Bob' to foo. 3 $ bar = & $ foo; // reference through $ bar. Note & symbol 4 $ bar = "My name is $ bar"; // modify $ bar 5 echo $ bar; 6 echo $ foo; // $ foo is also modified. 7?> |
Like other languages, variables with variable names can only be referenced.
Now you should have a rough understanding of the variable. now let's look at the indirect reference of the variable and the string connection.
① Indirect reference of variables: Let's look at an example first.
The code is as follows: |
|
$ A = "B "; $ A = "123 "; Echo $ B; ?>
|
The output above is 123
We can see that there is another $ in the second line of code, and the variable is accessed through the specified name, the specified name is stored in $ a ("B, change the value of $ B to 123. Therefore, such $ B variables are created and assigned values.
By adding the $ tag before the variable, you can increase the number of references at will.
② String connection: Let's look at an example first.
The code is as follows: |
|
$ A = "PHP 4 "; $ B = "powerful "; Echo $ a. $ B; ?>
|
Note that in PHP 4.2.0 and later versions, the default value of the PHP command register_globals is off. This is a major change in PHP. Setting the value of register_globals to off affects the validity of the predefined variable set within the global range. For example, to obtain the DOCUMENT_ROOT value, you must use $ _ SERVER ['document _ root'] instead of $ DOCUMENT_ROOT, as shown in the following figure, use $ _ GET ['id'] instead of $ id from URL http://www.example.com/test.php? Id = 3, or use $ _ ENV ['home'] instead of $ HOME to get the value of the environment variable HOME.
We can see the third line of the code, the English (sentence) number, which can be used to connect strings and convert them into merged new strings.
Super global variable |
Description |
$ GLOBALS |
Contains a reference to a variable that is valid globally for each current script. The key name of the array is the name of the global variable. Already exists in PHP 3$ GLOBALSArray. |
$ _ 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_VARSArray (still valid, but not used ). |
$ _ GET |
Variables submitted to the script through URL requests. Similar to the old array$ HTTP_GET_VARSArray (still valid, but not used ). |
$ _ POST |
Variables submitted to the script through the http post method. Similar to the old array$ HTTP_POST_VARSArray (still valid, but not used ). |
$ _ COOKIE |
Variables submitted to the script through HTTP Cookies. Similar to the old array$ HTTP_COOKIE_VARSArray (still valid, but not used ). |
$ _ FILES |
Variables submitted to the script by uploading the http post file. Similar to the old array$ HTTP_POST_FILESArray (still valid, but not used) |
$ _ ENV |
Variables submitted to the script in the execution environment. Similar to the old array$ HTTP_ENV_VARSArray (still valid, but not used ). |
$ _ REQUEST |
This array is not trustworthy because the GET, POST, and COOKIE mechanisms are used to submit scripts to variables. The existence or absence of all the variables contained in the array and the Order of the variables followPhp. iniThe variables_order configuration indicator in to define. This array does not have a corresponding version before PHP 4.1.0. SeeImport_request_variables (). |
$ _ SESSION |
The variable currently registered to the script session. Similar to the old array$ HTTP_SESSION_VARSArray (still valid, but not used) |
Constant:
A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). Constants are case sensitive by default. Constant identifiers are always capitalized.
Constant names and any other PHP labels follow the same naming rules. A valid constant name starts with a letter or underline followed by any letter, number, or underline. The regular expression is like this: [a-zA-Z_x7f-xff] [a-zA-Z0-9_x7f-xff] *
① It is the data that cannot be changed during program execution, and the scope of the constant is global.
② The constant name is similar to the variable, except that it does not contain the dollar symbol "$ ". A valid constant name must start with a letter or underline followed by any number of letters, numbers, or underscores.
③ In PHP, constants are uppercase letters and are classified into system constants and custom constants.
The system constants will be introduced later.
1. the default constant _ FILE _ refers to the FILE name and path of the PHP program;
2. the default constant _ LINE _ refers to the number of lines in the PHP program;
3. _ CLASS name;
Custom constant: defines a constant through the define () function,
Syntax format: bool define (string $ name, mixed $ value [, bool case _ $ insensitive])
Name: specifies the constant name.
Value: specifies the value of a constant.
Insensitive: specifies whether the constant name is case sensitive. If it is set to true, it is case insensitive. if it is set to false, it is case sensitive. If this parameter is not set, the default value is false.
// Valid constant name
Define ("FOO", "something ");
Define ("FOO2", "something else ");
Define ("FOO_BAR", "something more ");
// The constant name is invalid.
Define ("2FOO", "something ");
// The following definition is valid, but it should be avoided: (do not start with _ as a custom constant)
// Maybe one day, PHP will define a magic constant of _ FOO _.
// This will conflict with your code
Define ("_ FOO _", "something ");
?>
Several PHP "magic constants"
Name |
Description |
_ LINE __ |
The current row number in the file. |
_ FILE __ |
The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Since PHP 4.0.2,_ FILE __It always contains an absolute path (if it is a symbolic link, it is the absolute path after resolution), and the previous version sometimes contains a relative path. |
_ DIR __ |
The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalentDirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) = |
_ FUNCTION __ |
Function name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters. |
_ CLASS __ |
Class name (new PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. |
_ METHOD __ |
Class method name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ). |
_ NAMESPACE __ |
Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0) |
...