Differences between echo, print, and print_r functions

Source: Internet
Author: User
In PHP, echo and print functions are basically the same (output), but there are still slight differences between the two. echo outputs do not return values, but print returns values, if the execution fails, flase is returned.

1. differences between echo and print

The echo and print functions in PHP are basically the same (output), but there are still slight differences between the two. No return value after echo output, but print has a return value. if the execution fails, flase is returned. Therefore, it can be used as a common function. for example, after the following code is executed, the value of $ r is 1.

$ R = print "Hello World ";

This means that print is available in some complex expressions, but echo is not. However, because the echo statement does not require any value to be returned, the running efficiency of the echo statement in the code is slightly faster than that of the print statement.

Echo does not return values; print has a return value, and print always returns 1.

2. expression

Print can be used for complex expressions, but echo cannot. for example, print can be used in the following example:

  1.  

3. parameters

Echo can have multiple parameters, while print can have only one parameter.

If multiple parameters exist in echo, separate them with commas (,). you do not need to add parentheses for each parameter. the correct syntax is as follows:

  1. Echo "good", "for", "you ";

Note: If echo has multiple parameters, it is incorrect to enclose all parameters with only one parentheses. The following statement is incorrect:

  1. Echo ("good", "for", "you ");

Print can have only one parameter, for example:

  1. Print ("good for you ");
  2. Print "good for you ";

Echo and print are both output strings. The main difference between echo and print is that echo is faster than print because echo has no return value.

4. the print_r () function is only used to output arrays.

In php, the content of the arrays output by the print_r function is not arranged. To make the output look better. For example, arrays have multiple layers. To list segments, we can write as follows:

  1. $ A = array ('a' => 'apple', 'B' => 'bana', 'C' => array ('X', 'y ', 'Z '));
  2. Print_r ($ );
  3. ?>
  4. The above example will output:
  5.  
  6. Array
  7. (
  8. [A] => apple
  9. [B] => banana
  10. [C] => Array
  11. (
  12. [0] => x
  13. [1] => y
  14. [2] => z
  15. )
  16. )

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.