The functions and methods that are easily confused in php are: 1. The difference between echo and print. There is no return value after echo is output, but print has a return value; 2. The difference between include and require, [include()] is Conditionally included functions, while [require()] is unconditionally included.
The functions and methods that are easily confused are:
1. The difference between echo and print
The functions of echo and print in PHP are basically the same (output), but there are still subtle differences between the two. There is no return value after echo output, but print has a return value, and it returns false when its execution fails. Therefore, it can be used as an ordinary function. For example, after executing the following code, the value of variable$r
will be 1.
PHP code:
$r = print "Hello World";
This means that print can be used in some complex expressions, but echo cannot. However, because the echo statement does not require any value to be returned, the echo statement in the code runs slightly faster than the print statement.
2. The difference between include and require
The functions of include() and require() are basically the same (include), but in usage There are also some differences. include() is a conditional inclusion function, while require() is an unconditional inclusion function. For example, in the following code, if the variable$a
is true, the file a.php will be included:
PHP code:
if($a){ include("a.php"); }
and require() is the same as include( ) is different. Regardless of the value of $a, the following code will include the file a.php into the file:
PHP code:
if($a){ require("a.php"); }
In terms of error handling , use the include statement. If an include 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 Qifen literally: require means a very strong request or requirement.
3. require_once() and include_once() statements
I’m off topic, because they look similar, 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 need to be included, which can effectively avoid the error of repeated definition of functions or variables caused by including the same piece of code.
4. The difference between empty string (") and NULL
Both empty strings and NULL in PHP are stored with a value of 0, but their types are not the same. It's different. You can try echo gettype("); and echo gettype(NULL); and you will find that they print string and NULL respectively. 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 (integer). It can be seen that string ("), NULL and 0 are "equal values" but not equal types.
5.!isset The difference with empty
We can understand from the literal meaning: empty is to determine whether a variable is "empty", while isset is to determine whether a variable has been set. But there is one thing that is absolutely necessary here Note: when a variable value is 0, empty considers the variable to be equal to empty, which is equivalent to no setting. For example, when we detect the $id variable, when $id=0, use empty and isset to detect whether the variable $id Already configured, both will return different values: empty means there is no configuration, isset can get the value of $id, see the example below:
PHP code:
$id=0; empty($id)?print "我是空的":print "我是$id ."; //结果:我是空的 !isset($id)?print "我是空的":print "我是$id .";//结果:我是0
6. The difference between == (equal) and === (constant)
Review the fourth difference between the empty string ("") and NULL above, and let’s look at another one Example:
PHP code:
" == NULL; " === NULL;
After running, you will find that the first one is true, and the second one is false! It can be seen that == only compares whether the values are equal, and === Then it not only compares values, but also compares types, which is more strict.
7. The difference between PHP functions and methods
I always thought they were the same thing, until I saw After reading the thinkphp documentation, there are specifications for functions and methods, and I realized that they are not the same thing.
Methods are "functions" in classes and can only be called through objects.
Specially, classes The static method can be called directly through the class name.
The function usually mentioned should be a function in the global scope. After being introduced, it can be called directly anywhere.
The simple distinction is :
The function exists alone, that is, it is defined in the process-oriented part.
The method depends on the class, that is, it is defined in the object-oriented part.
Function , you can regard it as the implementation of an algorithm. The
method can be used as the implementation of business logic. The
class method can set access permissions and needs to be called through an object or class; function It is public and can be used by everyone.
Related learning recommendations:PHP programming from entry to proficiency
The above is the detailed content of What are the easily confused functions and methods in PHP?. For more information, please follow other related articles on the PHP Chinese website!