Parse some issues in PHP that may be ignored
The difference between print
The functions of echo and print in PHP are basically the same (output), but there are still slight differences between the two. There is no return value after echo output, but print has a return value, and returns flase when its execution fails. Therefore, it can be used as a normal function, for example, after executing the following code, the value of the variable $r will be 1.
$r = print "Hello World";
This means that print can be used in some complex expressions, while echo cannot. However, because the echo statement does not require any value to be returned, the echo statements already run slightly faster than the print statements.
The difference from require
The functions of include() and require() are basically the same (include), but they are also somewhat different in usage. include() is a conditional include function, while require() is an unconditional include function. For example in the following code, if the variable $a is true, the file will be included:
if($a){
include("");
}
require() is different from include(). No matter what value $a is taken, the following code will include the file in the file:
if($a){
require("");
}
In terms of error handling, use include statement. If an error occurs, the program will skip the include statement. Although the error message will be displayed, the program will continue to execute! But require will give you a fatal error.
Of course, we can also understand the seventh part of the literal meaning: require means a very tough request and a request.
3.Require_once() and include_once() statements
As a side note, because it looks like it, the simple require_once() and include_once() statements correspond to require() and include() statements respectively. The require_once() and include_once() statements are mainly used when multiple files are required, which can effectively avoid the error of repeatedly defining functions or variables when including the same piece of code.
4. The difference between empty string ('') and NULL
PHP hollow strings and NULL are stored with a value of 0, but their types are different. You can try echo gettype(''); and echo gettype(NULL); you will find that they print out string and NULL, and of course 0 is also easy to confuse. You can try echo gettype(0); print the type and you will find that the type of 0 is integer (integrity), and you can see that strings (''), NULL and 0 are "equal values" but not equal types.
The difference between empty
From the literal meaning we can understand: empty is to judge whether a variable is "empty", while isset is to judge whether a variable has been set. But there is one thing that must be paid attention to here: when a variable value is 0, empty believes that this variable is equal to empty, that is, it is equivalent to not setting. For example, when we detect the $id variable, when $id=0, use empty and isset to detect whether the variable $id has been configured, and both will return different values: empty believes that there is no configuration, and isset can obtain 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. The difference between ==(element) and ===(element)
Looking back at the difference between the fourth empty string ("") and NULL above, let's take a look at an example:
'' == NULL;
'' === NULL;
After running, you will find that the first one is true, while the second one is false! It can be seen that == is just about comparing whether the values are equal, while === not only compares the values, but also compares the types, which is more stringent.
The difference between :: and this->
When accessing member variables or methods in PHP classes, if the referenced variable or method is declared as const (defining a 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 you access const or static variables or methods from the inside of the class, you must use the self-referenced self. Otherwise, if you access const or static variables or methods from the inside of the class, you must use the self-referenced $this.
The difference between () and strpos()
strristr() case insensitive strstr() case sensitive
Functions find where the string first appears in another string.
If successful, the rest of the string is returned (from the matching point). If the string is not found, false is returned.
stripos() case insensitive strpos() case sensitive
The function returns the position where the string first appears in another string.
If the string is not found, false is returned.
After testing, it is proved that if you just search and determine whether it exists, the execution efficiency of strpos() is greater than strstr()
HTTP_HOST and SERVER_NAME
Similarities:
When the following three conditions are met, the same information will be output.
1. The server is port 80
2. ServerName is set correctly in apache conf
3. HTTP/1.1 protocol specification
Differences:
1. Normally:
_SERVER["HTTP_HOST"] Under the HTTP/1.1 protocol specification, information will be output based on the client's HTTP request.
_SERVER["SERVER_NAME"] By default, the ServerName value in the apache configuration file is directly output.
2. When the server is not port 80:
_SERVER["HTTP_HOST"] will output the port number, for example::8080
_SERVER["SERVER_NAME"] will output ServerName value directly
Therefore, in this case, it can be understood as: HTTP_HOST = SERVER_NAME: SERVER_PORT
3. When the ServerName in the configuration file is inconsistent with the domain name requested by HTTP/1.0:
The configuration is as follows:
ServerName
ServerAlias
Client access domain name
_SERVER["HTTP_HOST"] output
_SERVER["SERVER_NAME"] output
Therefore, in actual programs, you should try to use _SERVER["HTTP_HOST"] to be safer and more reliable.
If you are in port mapping and accessed on the intranet, it is better to use "$_SERVER['HTTP_X_FORWARDED_HOST']".
Related Articles
PHP obtains the root directory's __FILE__ constant
PHP's __FILE__ constant (how to get the root directory)2008-07-07Example of controllable concurrent asynchronous operation by php curl batch processing
This article mainly introduces the implementation of controllable concurrent asynchronous operations through php curl batch processing. In combination with examples, php uses curl's curl_multi_* family function to perform concurrent operations related implementation techniques. Friends who need it can refer to it2018-05-05Several ways to read database information by php
Connect to a mysql server with url address localhost and port 3306. The account number of mysql server is "root" and the password is "9999". There is a database on the mysql server, and there is a table abc in the database. The table abc has two columns, with the column names "id" and "name" respectively, and read out all the data in abc.2008-05-05Detailed explanation of the installation and use of eAccelerator
This article provides a detailed analysis and introduction to the installation and use of eAccelerator. For those who need it, please refer to it.2013-06-06PHP face object database operation class example
This article mainly introduces the php face object database operation class, and describes the techniques of encapsulating database operations through object-oriented packaging in the form of examples. It is very practical. Friends who need it can refer to it.2014-12-12PHP connection to MSSQL2008/2005 database (SQLSRV) configuration instance
This article mainly introduces the configuration method of PHP connection to MSSQL2008/2005 database (SQLSRV). The examples tell the complete connection and configuration process, and provide specific extension file download and test code. Friends who need it can refer to it.2014-10-10Solution to the problem that the php environment cannot upload files
For dedecms you did not choose to upload files. Discuz batch upload was successfully displayed, but the pictures were not visible, the ecshop mall, etc. cannot upload problems. As long as it is PHP, and there is no problem with the code itself. The following are applicable2014-04-04Example of simple adapter mode implemented by PHP
This article mainly introduces the simple adapter mode implemented by PHP, and analyzes the implementation skills and calling methods of the php adapter mode in combination with specific examples. Friends who need it can refer to it2017-06-06PHP instance method for counting the number of different elements in an array
In this article, the editor will sort out examples and methods for php statistics on the number of different elements in the array and related knowledge points. Friends in need will learn it.2019-09-09Summary of several common attack methods in PHP
This article mainly introduces several common attack methods in PHP. It summarizes and analyzes common attack methods such as php SQL injection, XSS attack, file inclusion vulnerabilities, etc. in combination with examples. Friends who need it can refer to it.2019-04-04