The difference between Php--echo and print

Source: Internet
Author: User
Tags echo command

This article mainly introduces the difference between Echo and print in PHP, and makes a thorough summary and analysis of the common usage differences between them, and the friends who need can refer to

In general, the dynamic output of HTML content in PHP is achieved through print and ECHO statements, and in practice, both print and ECHO functions are almost identical. It can be said that there is a place to use, and the other can be used. However, there is also a very important difference between the two: in the Echo function, you can output multiple strings at the same time, and in the print function you can only output one string at a time. At the same time, the Echo function does not require parentheses, so the echo function is more like a statement than a function.

Echo and print are not functions, but language constructs, so parentheses are not required

The difference between them is:

(1) echo can output multiple strings, like this:

Echo ' A ', ' B ', ' C ';

If you have to add parentheses, note that echo (' A ', ' B ', ' C ') is wrong and should be written as:

Echo (' A '), (' B '), (' C ');

It does not behave like a function, so it cannot be used in the context of a function

(2) print can only output a string, it may behave like a function, for example, you can use the following:

$ret Print ' Hello world ';

All of it can be used in more complex expressions.
Plus,Echo's efficiency is relatively fast!

Look at the following code:

<? PHP $a= ' Hello '; $b= ' php world! '; Echo $a,$b. ' <br/> '; // Echo can use a comma-delimited string variable to display Print $a. $b. ' <br/> '; // print can not use commas, can only be separated by Dot,//print $a, $b. ' <br/> ';//the use of the comma-times is wrong here. ?>

Analysis Summary:

The echo command is the same as the Print command, no difference
The Echo function differs from the print function.
Echo () No return value, same as echo command
Print () has a return value, success, return to 1,false, return 0.
printf () is similar to sprintf (), which is formatted output, but is output to standard output, which is output to a variable

Shaped like:

Echo  <<<print  <<< EOT EOT;

Written format, which has the following meanings:

The <<< operator, which treats the variables between the custom delimiters as strings,
EOT custom delimiter, must be at the beginning of the line at the end

It is believed that this article has some reference value for us to master PHP program design better.

The difference between Php--echo and print

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.