The difference between Echo and print in PHP _php tips

Source: Internet
Author: User
Tags echo command

In general, the dynamic output of HTML content in PHP is done through print and ECHO statements, and in practice, the 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: in the Echo function, you can output multiple strings at the same time, whereas in the print function you can output only one string at a time. Also, the Echo function does not require parentheses, so the echo function is more like a statement than a function.
Neither Echo nor print is a function, but a language structure, so parentheses are not required.

the difference between them is:

(1) echo can output multiple strings , as follows:

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

If you have to add parentheses, note that you write echo (' A ', ' B ', ' C '), and it is wrong to write:

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 can act like a function , for example, you can use the following:

$ret = print ' Hello world ';

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

Look at the following code:

<?php
$a = ' hello '; $b = ' php world! '; echo $a, $b. ' <br/> ';//echo you can use a comma-delimited string variable to display the
print $a. $b. ' <br/> ';/and print cannot use commas, only dots,
//print $a, $b. ' <br/> '//here use comma times wrong.
?>

Analysis Summary:

The echo command is the same as the Print command, no difference
There is a difference between the Echo function and the print function.
Echo () has no return value, same as echo command
Print () has a return value, success, return to 1,false, return 0.
printf () is similar to sprintf () and is formatted for output, unlike the former output to the standard output, which outputs to the variable

Shaped like:

echo  <<< EOT 
EOT; 
Print  <<< EOT 
EOT; 

Writing format, which has the following meanings:

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

I believe that this article on the better grasp of the PHP program design has a certain reference value.

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.