Ec (2); 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 way to set variables in PHP is: $ var_namevalue; PHP administrators 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: for changes to script ec (2) and script
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:
$ Var_name = value; PHP users 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:
If you define variables and constants, how many aspects will you pay attention? You may think:
How to define variables? What is the difference between variables and C?
Are Variables case sensitive?
Are there other important PHP variables?
Do constants and variables have the same definitions?
Let's talk about it separately.
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:
$ 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. Example: 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.
3. Other important points of PHP
Predefined Variables
Predefined variables are an important concept in PHP. PHP provides a large number of predefined variables. Because many of these variables depend on the version and settings of the running server, and other factors, there is no detailed description document. Some predefined variables do not take effect when PHP is run as a command line.
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? In id = 3, get the id value, or use $ _ ENV ['home'] instead of $ HOME to get the value of the environment variable HOME.
Starting from PHP 4.1.0, PHP provides an additional set of pre-defined arrays that contain data from the web server (if available), runtime environment, and user input. These arrays are very special and they take effect globally, for example, automatically within any range. Therefore, it is usually called autoglobals or superglobals ). (PHP does not have a mechanism for customizing global variables .) Hyperglobal variables are listed below. You will also notice that the old pre-defined array ($ HTTP _ * _ VARS) still exists. From PHP 5.0.0, you can set register_long_arrays to block long-format PHP predefined variables.
The following table lists the super global variables of PHP:
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. $ GLOBALS array exists from 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 $ HTTP_SERVER_VARS array (still valid, but not used ).
$ _ GET variables submitted to the script through URL requests. Similar to the old $ HTTP_GET_VARS array (still valid, but not used ).
$ _ POST variables submitted to the script through the http post method. Similar to the old $ HTTP_POST_VARS array (still valid, but not used ).
$ _ COOKIE variables submitted to the script through HTTP Cookies. Similar to the old $ HTTP_COOKIE_VARS array (still valid, but not used ).
$ _ FILES variables submitted to the script by uploading the http post file. Similar to the old array $ HTTP_POST_FILES array (still valid, but not used)
$ _ ENV: variables submitted to the script in the execution environment. Similar to the old $ HTTP_ENV_VARS array (still valid, but not used ).
$ _ REQUEST is submitted to the script variables through GET, POST, and COOKIE mechanisms, so this array is not trustworthy. All the existence or not of the variables contained in the array and the order of the variables are defined according to the variables_order configuration instructions in php. ini. This array does not have a corresponding version before PHP 4.1.0. See import_request_variables ().
$ _ SESSION: The variable currently registered to the script SESSION. Similar to the old array $ HTTP_SESSION_VARS array (still valid, but not used)
Application Scope of Variables
Every variable has an application scope. How is PHP defined? Let's take a look at the following code:
1 2 $ var = 0;
3 function test ($ index)
4 {
5 $ var = $ var + 1;
6 echo "The". $ index. "number is". $ var ."
";
7}
8 test (1 );
9 test (2)
10?> What do you think the above Code will display?
If you think it is the following:
Result 1:
The 1 number is 1
The 2 number is 2 sorry, your results are incorrect.
In fact, the correct result should be:
Result 2:
The 1 number is 1
The 2 number is 1 so what did you find out from it? We can know that although the Code of Line 1 is defined outside, the variables of Line 2 are different from those of it. The variables in the 5th rows are only used in this function. Further, if I want to call the variable in the first line and display the result 2, the code can be as follows:
1 2 $ var = 0;
3 function test ($ index)
4 {
5 global $ var;
6 $ var = $ var + 1;
7 echo "The". $ index. "number is". $ var ."
";
8}
9 test (1 );
10 test (2)
11?> What is the difference between this code segment and the above Code segment? Note that there is a global keyword in the 5th rows. See.
Is there any other way? The answer is yes.
The Code is as follows:
1 2 $ var = 0;
3 function test ($ index)
4 {
5
6 $ GLOBALS ["var"] = $ GLOBALS ["var"] + 1;
7 echo "The". $ index. "number is". $ GLOBALS ["var"]."
";
8}
9 test (1 );
10 test (2)
11?> Is there anything special about the code? That is, the hyper-global variable $ GLOBALS is used.
PHP also has the statement of static variables. However, static variables are generally used in functions and can only be local variables. Let's take a look at the following code:
1 2 function Test ()
3 {
4 static $ a = 0;
5 echo $ ."
";
6 $ a ++;
7}
8 Test ();
9 Test ();
10?> Result:
1
2
PHP also has an exciting feature: Variable variables.
A variable name can be dynamically set and used.
Take a look at the following example:
1 2 $ a = "hello ";
3 $ hello = "world ";
4 echo $ a. "". $;
5?> The output result is hello, world. That's amazing. $ A is actually $ hello, because the value of $ a is hello.
There are more variables. Let's take a look at constants.
Constant
Is const added before the constant of PHP? Let's take a look.
No. PHP must be defined in the following way.
Bool define (string name, mixed value [, bool case_insensitive])
Name is the constant name and value is the constant value. Case_insensitive] Is case sensitive. The default value is sensitive. For example:
1 2 define ("CONSTANT", "Hello world .");
3 echo CONSTANT; // outputs "Hello world ."
4 echo Constant; // outputs "Constant" and issues a notice.
5
6 define ("GREETING", "Hello you.", true );
7 echo GREETING; // outputs "Hello you ."
8 echo Greeting; // outputs "Hello you ."
9
10?>
Constants and variables are different:
There is no dollar sign before the constant ($ );
Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
Constants can be defined and accessed anywhere, regardless of the variable range rules;
Once defined, a constant cannot be redefined or canceled;
The constant value can only be a scalar.
Instance
$ Txt = "Hello World! ";
$ Number = 16;
?> PHP is a Loosely Typed Language)
In PHP, you do not need to declare the variable before setting it.
In the preceding example, you can see that you do not have to declare the Data Type of the variable to PHP.
PHP automatically converts a variable to a correct data type based on the method in which the variable is set.
In a strongly typed programming language, you must declare the type and name of the variable before using it.
In PHP, variables are automatically declared during use.
// Reference
$ One = "test ";
Two = & $ one; // equivalent to the transfer address. The two variables point to an address.
// Dynamic variables
$ One = "######";
$ Two = "one ";
$ Three = "two ";
Echo $ three. "<br>"; // output "two"
Echo $ three. "<br>"; // output "one"
Echo $ three. "<br>"; // output "######"
// Php has eight types
// Four scalar types: int integer
// Bool boolean
// Float, double, real
// String
// Two composite types: array
// Object
// Two special types: resource
// Null
// Integer Declaration
$ Int = 10; // decimal Declaration
$ Int = 045; // octal Declaration
$ Int = 0xff; // hexadecimal Declaration
$ Float = 3.14E + 5; // scientific notation
$ Float = 3.14E-5;
// When all values are false
$ Bool = false;
$ Bool = 0;
USD bool = 0.000;
$ Bool = null;
$ Bool = "";
$ Bool = "";
$ Bool = "0 ";
$ Bool = array ();
// String Declaration
// 1. single quotation marks and double quotation marks can both declare strings
// 2. The declared string has no length limit
// 3. In a double quotation mark string, both variables can be parsed directly and escape characters can be used directly (you can escape the single quotation mark itself or the Escape Character "")
// 4. Variables cannot be parsed directly or escape characters in single quotes.
// 5. You cannot use double quotation marks in double quotation marks.
// 6. It is best to use single quotes,
$ Str = 'aaaaa ';
$ Str = "aaaa ";
// The delimiters declare strings, a large number of strings
// Test is a custom string, which cannot contain any characters or spaces.
// It must end with the custom string "test". No character is allowed before the end.
$ Str = <test
This write content ......
Test;
?>