Parse some of the issues in PHP that might be overlooked

  • 2020-06-15 07:53:31
  • OfStack

1. The difference between echo and print
echo and print in PHP have basically the same functionality (output), but there are subtle differences between the two. echo outputs no return value, but print does, returning flase when its execution fails. Thus it can be used as a normal function, such that the value of the variable $r will be 1 after executing the following code.
$r = print "Hello World";
This means that print can be used in 1 complex expression, but echo cannot. However, because the echo statement does not require any value to be returned, the echo statement already runs slightly faster than the print statement in the code.

2. Differences between include and require
include() and require() also have the same functionality (including), but there are some differences in usage. include() is a conditional inclusion function, while require() is an unconditional inclusion function. For example, in the following code, the file a.php will be included if the variable $a is true:
if($a){
include("a.php");
}
require() is different from include(). Regardless of the value of $a, the following code will include the file a.php:
if($a){
require("a.php");
}
In the case of error handling, the include statement is used. If an error contains an error, the program skips the include statement and continues executing despite the error message! But requre will make a fatal mistake.
requre is a very strong request. It's not a very strong request.

3.require_once() and include_once() statements
As an aside, because of the long image, the 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 when you need to include more than one file, effectively avoiding the error of repeatedly defining functions or variables by including the same piece of code.

4. The difference between an empty string (') and NULL
Both PHP hollow string and NULL are stored with a value of 0, but they are not of the same type. You can try echo gettype("); And echo gettype (NULL); You'll notice that they print string and NULL, but the zeros are also confusing. Try echo gettype(0); Print the type under 1, and you'll see that the type of 0 is integer(integer), and you'll see that the strings (''), NULL, and 0 are of "equivalent" but different types.

5. Differences between isset and empty
empty determines if a variable is "empty," while isset determines if a variable is set. But here's one thing to be absolutely aware of: when a variable has a value of 0, empty considers the variable to be equal to null, meaning it is not set. For example, when we detect the $id variable, when $id=0, we use empty and isset to detect whether the variable $id is configured. Both will return different values: empty thinks it is not configured, isset can get the value of $id, see the following example:
$id=0;
empty ($id)? print "I am empty ":print" I am $id "; // The result: I am empty
! isset ($id)? print "I'm empty ":print" I'm $id "; // The result: I am 0

==(etc.) and ===(identity)
Recall the difference between the empty string ("") and NULL in # 4 above and consider another example:
'' == NULL;
'' === NULL;
After running it, you will find that the first one is true and the second one is false! You can see that == is just comparing whether the values are equal, and === is comparing not only the values, but also the type, which is more rigorous.

7. self: : and this - > The difference between
When accessing a member variable or method in an PHP class, if the referenced variable or method is declared as const(defining constant) or static(declaring static), then the operator :: must be used; otherwise, if the referenced variable or method is not declared as const or static, then the operator - must be used > .

In addition, if the const or static variable or method is accessed from inside the class, then the self-referenced self must be used, whereas if the access from inside the class is not the const or static variable or method, then the self-referenced $this must be used.

8. Difference between strstr() and strpos()
stristr() is case sensitive
The function finds the position of the first occurrence of a string in another string.
If successful, the rest of the string is returned (from the match point). If the string is not found, false is returned.
stripos() is case insensitive
The function returns the position of the first occurrence of a string in another string.
If the string is not found, false is returned.
It has been tested that the execution efficiency of strpos() is greater than that of strstr() if it is only to find and judge the existence of strpos().

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

Difference:
1. General conditions:
_SERVER["HTTP_HOST"] under the HTTP/1.1 protocol specification, output is based on the client's HTTP request.
_SERVER["SERVER_NAME"] by default prints the ServerName value in the httpd.conf configuration file for apache directly.

2. When the server is not port 80:
_SERVER["HTTP_HOST"] prints the port number, for example: mimiz.cn :8080
_SERVER["SERVER_NAME"] prints the ServerName value directly
So in this case, it can be understood as: HTTP_HOST = SERVER_NAME: SERVER_PORT

3. When ServerName in configuration file ES224en.conf does not send the domain name requested by HTTP/1.0:
httpd. conf configuration is as follows:
ServerName mimiz.cn
ServerAlias www.mimiz.cn
Client access domain www.mimiz.cn
_SERVER. [" HTTP_HOST "] output www mimiz. cn
_SERVER [" SERVER_NAME "] output mimiz. cn
Therefore, in practice, try to use _SERVER["HTTP_HOST"] for safety and reliability.
"$_SERVER['HTTP_X_FORWARDED_HOST']" is better if in the case of port mapping and access on the Intranet.


Related articles: