In php the difference between echo of and print of require of and include of is easily confused

  • 2020-05-12 02:25:10
  • OfStack

1. The difference between echo and print

The functions of echo and print in PHP are basically the same (output), but there are slight differences between the two. echo does not return a value after its output, but print does and returns flase when its execution fails. So it can be used as a normal function, for example, the variable $r will have a value of 1 after executing the following code.

$r = print "Hello World";

This means that print can be used in complex expressions, while echo cannot. However, because the echo statement does not require any numeric value to be returned, the echo statement runs slightly faster in the code than the print statement.

2. Differences between include and require

The functions of include() and require() are basically the same (inclusive), 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:

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

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

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

In terms of error handling, use the include statement. If an inclusion error occurs, the program will skip the include statement. The error message will be displayed but the program will continue to execute! But requre will make you a fatal mistake.

requre is a very strong request or request.

3. Statements require_once() and include_once()

As an aside, the simple require_once() and include_once() statements correspond to the require() and include() statements, respectively, because of the long image. The require_once() and include_once() statements are mainly used when multiple files need to be included and can effectively avoid the mistake of repeatedly defining functions or variables by including the same piece of code.

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

PHP hollow strings and NULL are both stored with values of 0, but they are of different types. You can try echo gettype("); And echo gettype (NULL); You'll notice that they printed string and NULL, and of course 0 is also confusing. You can try echo gettype(0); If you print a type 1, you will find that the type of 0 is integer (integer), and you can see that the string ("), NULL, and 0 are "equivalent" but not of the same type.

5. Difference between isset and empty

We can understand from the literal meaning: empty is to judge whether a variable is "empty", and isset is to judge whether a variable has been set. But here's one thing to absolutely note: when a variable has a value of 0, empty considers that variable to be null, which means 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 has been configured. Both of them will return different values: empty thinks there is no configuration, while isset can get the value of $id. See the following example:
$id=0;
emptyempty ($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 == (etc.) and === (identical)

Recall the difference between the empty string ("") and NULL in clause 4 above, and take a look at another example:

'' == NULL;
'' === NULL;

When you run it, you'll find that the first one is true, and the second one is false! You can see that == is just comparing the values to be equal, and === is comparing not only the values, but also the types, more strictly.

Related articles: