Echo and print and print_r function Differences _php Tutorial

Source: Internet
Author: User
This article introduces the differences between the three output functions echo and print and Print_r, let's take a look at the usage speed issue.

The difference between 1.echo and print

The functions of Echo and print in PHP are basically the same (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. It can therefore be used as a normal function, such as executing the following code after the variable "value will be 1."

$r = print "Hello world";

This means that print can be used in some complex expressions, while Echo is not. However, because the Echo statement does not require any values to be returned, the Echo statement in the code is running more efficiently than the print statement.


ECHO has no return value; Print has a return value, and the return value of print is always 1.

An expression
Print can be used for complex expressions, and Echo is not. For example, print can be used for the following examples:

The code is as follows Copy Code

Parameters
Echo can have multiple parameters, and print can have only one parameter.

echo if there are multiple parameters, apply commas separated, each parameter does not need parentheses, the correct wording is as follows:

The code is as follows Copy Code
echo "Good", "for", "You";

Note that if ECHO has multiple parameters, it is wrong to use just one parenthesis to enclose all the arguments. The following syntax is incorrect:

The code is as follows Copy Code
Echo ("Good", "for", "You");

Print can have only one parameter, such as:

The code is as follows Copy Code

Print ("Good for You");
Print "Good for You";

Both Echo and print function as output strings. The main difference between Echo and print is that ECHO is faster than print because ECHO has no return value.


The Print_r () function is used only for output arrays.

The array contents of the Print_r function output in PHP are not listed. In order to make it more beautiful to output. For example, arrays have multiple layers. The segments are listed, and we can write this:

Example #1 Print_r () Example

The code is as follows Copy Code


$a = Array (' a ' = = ' Apple ', ' B ' + = ' banana ', ' c ' = = Array (' x ', ' y ', ' z '));
Print_r ($a);
?>

The above example would output:


Array
(
[A] = Apple
[B] = Banana
[c] = = Array
(
[0] = X
[1] = y
[2] = Z
)
)

http://www.bkjia.com/PHPjc/632227.html www.bkjia.com true http://www.bkjia.com/PHPjc/632227.html techarticle This article introduces the differences between the three output functions echo and print and Print_r, let's take a look at the usage speed issue. The difference between 1.echo and print is basically the same for the Echo and print functions in PHP (... ).

  • 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.