PHP (3) _ PHP tutorials

Source: Internet
Author: User
Tags constant definition
Those years of learning PHP together (3 ). 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. Below 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.

Function Echo () Print () Printf () Sprintf ()
Return Void Int: ever return 1 Int: String
Express Echo (string argument) Print (argument) Printf (string format) Printf (string format)
Instruction Write the string. Write the string. The return value is 1, which is used to verify whether the display is successful. Supports output format strings. for the format, see the following description. String in the same format, but not directly rendered in the browser

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:
Type Description Example
% B Consider the parameter as an integer and display its binary number Printf (% d, 10) ;=====> display 1010
% C Consider a parameter as an integer and display its ASCII characters Printf (% c, 65) ;=====> show
% D The parameter is regarded as an integer and displayed in decimal format. Printf (% d, 10); ======> 10
% F Consider the parameter as a floating point number and display its floating point number Printf (% f, 2); ========> 2.00000
% O Consider the parameter as an integer and display its octal value Printf (% o, 8) =============== 10
% S Consider the parameter as a string and display its string Printf (% s, "this is a TV") ====> this is a TV
% U Considers the parameter as an integer and displays an unsigned decimal number. Printf (% u,-100) ====> 100
% X Consider the parameter as an integer in hexadecimal notation in lower case. Omitted
% X Consider the parameter as an integer in uppercase. Omitted
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.

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. Below...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.