Fundamentals: PHP easy to distinguish between the function of the distinction between methods and significance

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags code content copy difference echo error example file

The difference between 1.echo and print

Echo and print functions in PHP are basically the same (output), but there are still some subtle differences between the two. echo output does not return a value, but print returns the value, when its implementation fails to return flase. Therefore, it can be used as a normal function. For example, after executing the following code, the value of variable $ r will be 1.

Copy PHP content to the clipboard

PHP code:

$ r = print "Hello World";

This means that print can be used in complex expressions, but echo does not. However, because the echo statement does not require any return value, the echo statement has run slightly faster in code than the print statement.

The difference between 2.include and require

The include () and require () functions are basically the same (include), but there are some differences in their usage. include () is a conditional include function and require () is an unconditional include function. For example, in the following code, if the variable $ a is true, the file a.php will be included:

Copy PHP content to the clipboard

PHP code:

if ($ a) {

include ("a.php");

}

The require () and include () is different, no matter what the value of $ a, the following code will be included in the file a.php file:

Copy PHP content to the clipboard

PHP code:

if ($ a) {

require ("a.php");

}

In the error handling, the use of include statement, if an include error occurs, the program will skip the include statement, although the error message will be displayed, but the program will continue to perform! But requre will give you a fatal error.

Of course, literally we can also understand seven points: requre is very tough request, the meaning of the request.

3.require_once () and include_once () statement

Off the hook, 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 you need to include more than one file, and you can effectively avoid the error that the same code is included and the function or variable is repeatedly defined.

4. Empty string (") and the difference between NULL

PHP hollow strings and NULLs are all stored with a value of 0, but their types are not the same. You can try echo gettype ("); and echo gettype (NULL); you will find that they are printed as string And NULL, of course, there are 0 is also easy to confuse, you can try echo gettype (0); Print type, you will find that the type of 0 is integer (integer), visible string Value "but not the same type.

5. What is the difference between isset and empty

Literally, we can understand: empty is to determine whether a variable is "empty", and isset to determine whether a variable has been set. But here is a point to be absolutely aware of: When a variable has a value of 0, empty considers the variable to be empty, which is equivalent to not setting. For example, when we detect $ id variables, when $ id = 0, empty and isset to detect whether the variable $ id has been configured, both will return different values: empty that is not configured, isset can get the value of $ id Look at the following example:

Copy PHP content to the clipboard

PHP code:

$ id = 0;

empty ($ 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

The difference between == (and so on) and == (equal)

Looking back to the difference between the fourth empty string ("") above and NULL, let's look at an example:

Copy PHP content to the clipboard

PHP code:

"== NULL;

"=== NULL;

After running you will find the first one is true, while the second one is false! Visible == just compare the value is equal, and === not only compare the value, but also more types, more stringent.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.