PHP Confusing Function Difference and Usage Summary

  • 2021-08-05 09:26:25
  • OfStack

In this paper, the differences and usage of PHP confusing functions are analyzed with examples. Share it 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 subtle differences between them. echo has no return value after output, but print has a return value and returns flase when its execution fails. Therefore, it can be used as a normal function, such as the variable $r will have a value of 1 after executing the following code.

PHP code:

$r = print "Hello World";

This means that print can be used in a number of complex expressions, while echo cannot. However, because the echo statement does not require any numeric 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 basically the same function (inclusion), 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, if the variable $a is true, the file a. php will be included:

PHP code:

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

require () is different from include (). Whatever the value of $a, the following code will include the file a. php:

PHP code:

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

In error handling, the use of include statement, if included errors, the program will skip the include statement, although the error message will be displayed but the program will continue to execute! But requre will give you a fatal mistake.
Of course, we can also understand 7 points literally: requre is a very tough request, asking for the meaning.

3. require_once () and include_once () statements

This is a digression, because the simple require_once () and include_once () statements correspond to require () and include () statements, respectively. require_once () and include_once () statements are mainly used when multiple files need to be included, which can effectively avoid the error of duplicate definition of functions or variables when the same code is included.

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

PHP empty 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 respectively. Of course, 0 is also easy to confuse. You can try echo gettype (0); If you print the type under 1, you will find that the type of 0 is integer (integer type), and the visible string ("), NULL and 0 are" equivalent "but not equal types.

5.! Difference between isset and empty

Literally, we can understand that empty is to judge whether a variable is "empty", while isset is to judge whether a variable has been set. But here's one thing to be absolutely aware of: When a variable has a value of 0, empty thinks that the variable is equivalent to null, that is, it is equivalent to not being set. For example, when we detect the variable $id, when $id=0, we use empty and isset to detect whether the variable $id has been configured, and both will return different values: empty thinks it has not been configured, and isset can get the value of $id. See the following example:

PHP code:

$id=0;
empty($id)?print " I'm empty ":print " I am $id ."; // Result: I am empty
!isset($id)?print " I'm empty ":print " I am $id .";// Result: I am 0

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

Looking back at the difference between the empty string ("") in number 4 above and NULL, let's look at another 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 values are equal, while = = = compares not only values but also types, which is more strict.

I hope this article is helpful to everyone's php programming.


Related articles: