Differences between echo and print functions and print_r functions

Source: Internet
Author: User
Tags arrays

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.

Expression
Print can be used for complex expressions, but echo cannot. For example, print can be used as follows:

The code is as follows: Copy code
<Html> <body> <? Php $ a = true; $? Print "true": print "false";?> </Body>

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:

The code is as follows: Copy code
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:

The code is as follows: Copy code
Echo ("good", "for", "you ");

Print can have only one parameter, for example:

The code is as follows: Copy code

Print ("good for you ");
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.


Print_r () function, used only 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:

Example #1 print_r () example

The code is as follows: Copy code

<Pre>
<? Php
$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => array ('X', 'y ', 'Z '));
Print_r ($ );
?>
</Pre>
The above example will output:

<Pre>
Array
(
[A] => apple
[B] => banana
[C] => Array
        (
[0] => x
[1] => y
[2] => z
        )
)
</Pre>

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.