After understanding the PHP variables, you must understand the use of rules, definitions, output methods, and storage methods before using variables.
After understanding the PHP variables, you must understand the use of rules, definitions, output methods, and storage methods before using variables.
I. PHP data output
PHP supports a wide range of data types. Later, I will use it again and again. I have knowledge about other languages, so I can better understand the uniqueness of PHP. The following describes the PHP output. The basic output methods of PHP to the browser include Echo (), Print (), printf (), and sprintf (). We can compare the above four output methods.
In fact, the difference between Echo () and print () is not big. Which one to use depends entirely on your preferences, and the latter two are the same. What is formatted string output? When there is a C/C ++ language, we will understand this meaning, that is, we can properly format the symbols for output formatting.
Ii. formatted output format used in PHP
The following table lists the formatting symbols:
Iii. Notes for variable declaration in PHP
In php, the declaration of variables is similar to the shell script language. All variables start with the $ symbol. Note the following points:
1): $ is always before the variable, and the variable is a valid identifier.
2): variables are case sensitive. For example, $ Book and $ book are different.
3): PHP variables do not need to be declared, which is exactly the opposite of the C language.
4): the variable can be assigned a value after being declared. The value assignment is divided into value assignment, and the reference value assignment is the stack address assignment.
Iv. Scope of variables in PHP
Variables include local variables, global variables, static variables, PHP, and unique super global variables by scope. Local variables can only be used in declared scopes, and global variables can be used throughout the lifecycle. Static variables are declared to use static modifiers. After the function exits, Static variables still exist in the memory. For example
The Code is as follows:
Funtion keep ()
{
Static $ count = 0;
$ Count ++;
Echo $ count;
Echo"
";
}
10:
11: keep (); // output 1
12: keep (); // output 2
13: keep (); // output 3
14: keep (); // output 4
15:
16: // you may think that all the output values are 1, but they are exactly 1234. Here is the static effect.
17:?>
V. Super global variables ($ _ SERVER, $ _ GET, $ _ POST, $ _ COOKIE), $ _ FILES, $ _ ENV, $ _ SESSION
Let's take a look at the Super global variables. PHP provides many useful pre-defined variables that can be accessed at any location where the script can be executed. It is used to provide a large amount of environment-related information and obtain the session and operating environment of the current user, local environment. For example, you can use
The Code is as follows:
Foreach ($ _ SERVER as $ var => $ value)
{
// Print and output all system super Variables
Echo "$ var => $ value
";
}
We can see that many system variables are output.
HTTP_HOST
=>
Localhost
And so on.
We can use
$ _ SERVER ["HTTP_HOST"]
To obtain these global variables. $ _ SERVER global variables include WEB servers, customer configurations, and current information, which can be used by searching documents.
You can also GET the passed variables through the GET method. The $ _ GET super global variable contains the relevant information about the parameters of the GET Method for trial. For example, the requested URL address is a http://www.baidu.com/index.html? Cat = apache & id = 145, you can use the super global variable to access the following variables: $ _ GET ['cat'] = "apache "; $ _ GET ['id'] = "145". By default, you need to access the variables passed through the GET method. $ _ GET is the only way to access the super global variables, you cannot reference GET variables in the form of $ cat or $ id. For more information, see the section on secure access to external data.
In addition, the POST method can also pass variables.
The details are as follows: The $ _ POST super global variable contains information about passing parameters using the POST method.
Consider the following request form:
The Code is as follows:
The following POST variables can be used through the target script a. php:
$ _ POST ['email '] = "zyl0395@126.com ";
$ _ POST ['pswd '] = "Bestyear ";
We can also use the super global variable to save COOKIE information. $ _ COOKIE stores all the Information uploaded to the script in HTTPcookie. These cookies are generally used by the previously executed PHP script through the PHP function setcookie () set. For example:
The Code is as follows:
$ Value = 'somewhere ';
Setcookie ("TestCookie", $ value );
Setcookie ("TestCookie", $ value, time () + 3600);/* one-hour valid cookie */
Setcookie ("TestCookie", $ value, time () + 3600 ,"/~ Rasmus/"," example.com ", 1 );
?>
It doesn't matter if you don't understand it here. Later, we will study cookie knowledge.
The hosts file, the server needs to obtain the relevant information of the file, it is obtained through the variable $ _ files. There are five elements:
1): $ _ FILES ['userfile'] ['name']
The original name of the client machine file.
2): $ _ FILES ['userfile'] ['type']
The MIME type of the file, which must be supported by the browser, for example, "image/gif ".
3): $ _ FILES ['userfile'] ['SIZE']
Size of the uploaded file, in bytes.
4): $ _ FILES ['userfile'] ['tmp _ name']
Temporary File Name stored on the server after the file is uploaded.
5): $ _ FILES ['userfile'] ['error']
The Error Code related to the file upload. ['Error'] is added in PHP 4.2.0.
$ _ EVN is the information used by the PhP server, and $ _ SESSION obtains the SESSION information.
6. PHP constant definition
Constants are the amount that cannot be changed in the program. They are very useful, such as the circumference rate.
Definition: define ("PI", 3.1415926)
Use echo PI;
7. I will not talk about the logic symbols, operation levels, expressions, process control, and logic in PHP. It is basically consistent with the C ++ language. Here is just a brief description of what is missing. For example, the role of Include in PHP.
Include is also used to introduce sentences Containing files in PHP. The basic syntax is include (/path/to/file) to reference/user/local/lib/php/wjgilmore/init. inc. php is like this:
The Code is as follows:
Include "/user/local/lib/php/wjgilmore/init. inc. php ";
?>
Note that
The include statement must be defined by braces {}.
Otherwise, an error occurs ., you can also use include to reference a remote file. if the server where the file is located supports PHP, pass the necessary key-value pairs (similar to the GET request method, the included variables will also be parsed)
Example: include "http://www.123.com/index.html? Background = red ";
If it is referenced only once, use
Include_once
First, it checks whether the file is referenced. If not, it is referenced. If yes, It is not executed.
Include_once (),
Make sure once.
The same method: require is a request file, and require_once is a request. Detailed explanation of the time used later.