Those years of learning PHP together (3)

Source: Internet
Author: User
Tags constant definition

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 exampleCopy codeThe Code is as follows: <? Php
Funtion keep ()
{
Static $ count = 0;
$ Count ++;
Echo $ count;
Echo "<br/> ";
}
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 useCopy codeThe Code is as follows: foreach ($ _ SERVER as $ var => $ value)
{
// Print and output all system super Variables
Echo "$ var => $ value <br/> ";
}

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:Copy codeThe Code is as follows: <form action = "a. php" method = "post">
<P/> Email-adress: <input type = "text" name = "email" size = "20" maxlength = "50" value = "">
<P/> Password: <br/>
<Input type = "password" name = "pswd" size = "20" maxlength = "15" value = "">
<Br/>
<Input type = "submit" name = "submit" size = "20" maxlength = "50" value = "LOGIN">
</Form>

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:Copy codeThe Code is as follows: <? Php
$ 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:Copy codeThe Code is as follows: <? Php
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.

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.