Differences between echo and print in PHP

Source: Internet
Author: User
Tags echo command
This article mainly introduces the differences between echo and print in PHP, and provides a more in-depth summary and analysis on the common usage differences between the two. For more information, see, PHP dynamically outputs HTML content through print and echo statements. in actual use, print and echo functions are almost identical. In this case, you can use either of them. However, there is also a very important difference between the two: in the echo function, multiple strings can be output at the same time, while in the print function, only one string can be output at the same time. At the same time, the echo function does not need parentheses, so the echo function is more like a statement than a function.
Echo and print are not functions, but language structures, so parentheses are not required.

Their differences are:

(1) echo can output multiple strings, as shown below:

echo 'a','b','c';

If you need to add parentheses, be sure to write echo ('A', 'B', 'C'); it is incorrect and should be written:

echo ('a'),('b'),('c');

It does not act like a function, so it cannot be used in the context of the function.
(2) print can only output one string, which can behave like a function. for example, you can use the following:

$ret = print 'Hello World';

It can be used in more complex expressions.
In addition, echo is relatively efficient!

Take a look at the following code:

<? Php $ a = 'hello'; $ B = 'php world! '; Echo $ a, $ B .'
'; // Echo can use commas to separate string variables to display print $ a. $ B .'
'; // And print cannot use commas. they can only be separated by dots. // print $ a, $ B .'
'; // If you use a comma, an error is returned.?>

Analysis Summary:

The echo command is the same as the print command.
The echo function differs from the print function.
Echo () has no return value, which is the same as the echo command.
Print () has a return value, success, return 1, false, return 0.
Both printf () and sprintf () are formatted outputs. The difference is that the former is output to the standard output, and the latter is output to the variable.

Shape:

echo  <<< EOT EOT; print  <<< EOT EOT; 

The format, meaning:

<Operator, which treats the content between custom delimiters as strings and processes the variables
EOT user-defined delimiters. the end must be at the beginning of the line.

I believe this article provides some reference value for you to better master PHP programming.

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.