Home > Backend Development > PHP Tutorial > Summarize the confusing function usage in PHP

Summarize the confusing function usage in PHP

巴扎黑
Release: 2023-03-14 17:04:02
Original
1016 people have browsed it

This article mainly introduces the differences and usage of confusing functions in PHP. Examples summarize functions such as echo and print, include and require, !isset and empty. It is a very practical skill. Friends who need it can refer to it

This article analyzes the differences and usage of easily confused functions in PHP with examples. Share it with everyone for your reference. The specific analysis is as follows:

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 a normal function. For example, after executing the following code, the value of variable $r will be 1.

PHP code:

The code is as follows:

$r = print "Hello World";
Copy after login


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 there are some differences in usage. include() is a conditional inclusion function, while require() unconditionally contains a function. For example, in the following code, if the variable $a is true, the file a.php will be included:

PHP code:

The code is as follows:

if($a){
include("a.php");
}
Copy after login


Require() is different from include(). No matter what value $a takes, the following code will include the file a.php into the file:

PHP code:

The code is as follows:

if($a){
require("a.php");
}
Copy after login

In terms of error handling, use the include statement. If an inclusion error occurs, the program will skip the include statement. Although the error message will be displayed, the program will continue to execute! But the requre will give You made a fatal mistake.
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. The simple require_once() and include_once() statements correspond to require() and include() respectively. statement. The require_once() and include_once() statements are mainly used when multiple files need to be included, which can effectively avoid errors in repeated definitions of functions or variables caused by including the same piece of code.

4. The difference between empty string (") and NULL

Empty strings and NULL in PHP are both stored with a value of 0, but their types are different. You can try Check echo gettype("); and echo gettype(NULL); and you will find that what they print is string and NULL respectively. Of course, 0 is also easy to confuse. You can try echo gettype(0); to print the type. It will It is found that the type of 0 is integer (integer). It can be seen that string ("), NULL and 0 are "equal values" but not of equal types.

5.! The difference between isset and 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 you must pay attention to: when a variable value is 0, empty considers it. This variable is equivalent to empty, which means it is not set. For example, when we check the $id variable, when $id=0, we use empty and isset to check whether the variable $id has been configured. Both will return different values: empty. If there is no configuration, isset can get the value of $id. See the example below:

PHP code:

The code is as follows:

$id=0;
empty($id)?print "我是空的":print "我是$id ."; //结果:我是空的
!isset($id)?print "我是空的":print "我是$id .";//结果:我是0
Copy after login

6.==( The difference between etc.) and === (equal)

Review the difference between the fourth empty string ("") and NULL above, let's look at another example:

PHP code:

The code is as follows:

" == NULL;
" === NULL;
Copy after login


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 === not only compares values, but also compares types, which is more strict

.

The above is the detailed content of Summarize the confusing function usage in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template