Resolve some problems that may be ignored in PHP _ PHP Tutorial

Source: Internet
Author: User
Resolve some problems that may be ignored in PHP. 1. differences between echo and print: echo and print functions in PHP are basically the same (output), but there are still slight differences between the two. No return value after echo output, but print has a return value. 1. differences between echo and print
The echo and print functions in PHP are basically the same (output), but there are still slight differences between the two. No return value after echo output, but print has a return value. if the execution fails, flase is returned. Therefore, it can be used as a common function. for example, after the following code is executed, the value of $ r is 1.
$ R = print "Hello World ";
This means that print is available in some complex expressions, but echo is not. However, because the echo statement does not require any value to be returned, the running efficiency of the echo statement in the code is slightly faster than that of the print statement.

2. Differences Between include and require
The include () and require () functions are basically the same (include), but there are also some differences in usage. include () is a conditional include function, while require () it is an unconditional include function. For example, in the following code, if the variable $ a is true, it will contain the file a. php:
If ($ ){
Include ("a. php ");
}
The require () is different from include (). No matter what the value of $ a is, the following code will include file a. php into the file:
If ($ ){
Require ("a. php ");
}
For error handling, use the include statement. If an include error occurs, the program skips the include statement. Although the error message is displayed, the program continues to execute! But requre will give you a fatal error.
Of course, we can also understand the literal meaning of 7 points: requre is a very tough request and requirement.

3. require_once () and include_once () statements
Because of the long Image, simple require_once () and include_once () statements correspond to the require () and include () statements respectively. The require_once () and include_once () statements are mainly used to avoid repeated function or variable definition errors when you need to include multiple files.

4. differences between NULL strings ('') and NULL
PHP empty strings and NULL are both stored with 0 values, but their types are not the same. you can try echo gettype (''); and echo gettype (NULL ); you will find that the printed values are string and NULL, and of course there are also 0 which are easy to confuse. you can try echo gettype (0); print the type, the type of 0 is integer (integer). It can be seen that the string (''), NULL, and 0 are" equivalent "but not equal to the type.

5. differences between isset and empty
Literally, we can understand that empty is used to determine whether a variable is "null", while isset is used to determine whether a variable has been set. However, when the value of a variable is 0, empty considers the variable to be null, that is, it is equivalent to no setting. For example, when we detect the $ id variable, when $ id = 0, empty and isset are used to check whether the variable $ id has been configured. both of them return different values: empty considers that there is no configuration, and isset can get the value of $ id. See the following example:
$ Id = 0;
Empty ($ id )? Print "I am empty": print "I am $ id."; // result: I am empty
! Isset ($ id )? Print "I am empty": print "I am $ id."; // result: I am 0

6. Differences Between = (wait) and = (constant)
Let's review the difference between the fourth NULL string ("") and NULL. let's look at an example:
''= NULL;
''= NULL;
After running, you will find that the first is true, and the second is false! It can be seen that = only compares whether the values are equal, while = compares not only the values, but also the types and stricter.

7. difference between self: And this->
When accessing the member variables or methods in the PHP class, if the referenced variables or methods are declared as const (defining constants) or static (declaring static), the operator must be used:: if the referenced variable or method is not declared as const or static, the operator-> must be used.

In addition, if you access const or static variables or methods from the inside of the class, you must use self-referenced. Otherwise, if the internal access from the class is not a const or static variable or method, then you must use the self-referenced $ this.

8. differences between strstr () and strpos ()
Stristr () is case insensitive strstr () is case sensitive
The function is used to locate the first occurrence of a string in another string.
If successful, the rest of the string is returned (from the matching point ). If this string is not found, false is returned.
Stripos () is case insensitive strpos () is case sensitive
The function returns the position where the string first appears in another string.
If this string is not found, false is returned.
Tests show that strpos () is more efficient than strstr ()

9. HTTP_HOST and SERVER_NAME in PHP
Similarities:
When the following three conditions are met, the two will output the same information.
1. the server is port 80.
2. set ServerName correctly in apache conf.
3. HTTP/1.1 Protocol specification

Differences:
1. general situation:
_ SERVER ["HTTP_HOST"] under the HTTP/1.1 protocol specification, information is output according to the client's HTTP request.
_ SERVER ["SERVER_NAME"] by default, the ServerName value in the configuration file httpd. conf of apache is directly output.

2. when the server is not port 80:
_ SERVER ["HTTP_HOST"] outputs the port number, for example, mimiz.cn: 8080.
_ SERVER ["SERVER_NAME"] directly outputs the ServerName value
Therefore, it can be understood as: HTTP_HOST = SERVER_NAME: SERVER_PORT

3. when the ServerName in the configuration file httpd. conf is inconsistent with the domain name of the HTTP/1.0 request:
Httpd. conf is configured as follows:
ServerName mimiz.cn
ServerAlias www.mimiz.cn
Client Access domain name www.mimiz.cn
_ SERVER ["HTTP_HOST"] outputs www.mimiz.cn
_ SERVER ["SERVER_NAME"] outputs mimiz.cn
Therefore, in actual programs, _ SERVER ["HTTP_HOST"] should be used as much as possible, which is relatively safe and reliable.
For Port ING and intranet access, it is better to use "$ _ SERVER ['http _ X_FORWARDED_HOST.

Echo and print functions are basically the same (output) in PHP, but there are still slight differences between the two. No return value after echo output, but print has a return value when it executes...

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.