PHP echo of and print of require of and include of function distinction

  • 2020-03-31 20:33:07
  • OfStack

1. The difference between echo and print

Echo and print do the same thing in PHP (output), but there are subtle differences between the two. There is no return value after the echo output, but print has a return value 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, but echo cannot. However, because the echo statement does not require any value to be returned, the echo statement already runs slightly faster in the code than the print statement.

2. The difference between include and require

Include () is basically the same as require(), but there are some differences 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 a.hp will be included:

{if ($a)        
Include (" Amy polumbo HP ");        
}    

Unlike include(), the following code will include the file a.hp, regardless of the value of $a:

{if ($a)        
Require (" Amy polumbo HP ");        
}    

For error handling, use the include statement. If an include error occurs, the program will skip the include statement and continue executing despite the error message! But requre can make a fatal mistake.

Of course, we can also take it literally: requre is a very strong request.

3. Require_once () and include_once() statements

Digression, because of the long image, the simple require_once() and include_once() statements correspond to the require() and include() statements, respectively. The require_once() and include_once() statements are mainly used when multiple files need to be included, effectively avoiding the error of repeatedly defining functions or variables by including the same code.

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

PHP hollow strings and NULL are stored with a value of 0, but they are of different types. 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 confusing. You can try echo gettype(0); If you print the type, you will see that the type of 0 is integer, and that the string ("), NULL, and 0 are "equivalent" but unequal.

5. The difference between isset and empty

We can see from the literal meaning: empty is to determine whether a variable is "empty", while isset is to determine whether a variable has been set. However, there is one absolute thing to note here: when a variable has a value of 0, empty assumes that the variable is equal to 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 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 fourth empty string (" ") and NULL above, and take a look at another example:

"= = NULL;      
"= = = NULL;    

After running it, you will find that the first one is true and the second one is false! You can see that == just compares the values to be equal, and === not only compares the values, but also compares the types, which is even stricter.


Related articles: