Differences between ECHO and print

Source: Internet
Author: User

The ECHO and print functions in PHP are basically the same (output), but there are still slight differences between the two.

1. Return Value

No return value after Echo output.

Print has a return value. If the execution fails, flase is returned.

Therefore, print can be used as a common function, which means that print can be used in some complex expressions, while ECHO does not. However, because the echo statement does not require any value to be returnedCodeThe echo statement in is slightly faster than the print statement.

2. Output string

ECHO can output multiple strings at the same time.

 
Echo $ Test1. 'And '.$ Test2;

Print () can only output one string. It must be enclosed in parentheses. The print () usage will perform special processing on the % of the output content.

There is no big difference between print () and print. One is a function and the other is a command.

 
Print($ Test);

Print can output large HTML segments without escaping the quotation marks.

<?Print<DND<HTML xmlns = "http://www.w3.org/1999/xhtml"> DND?>

Print has only one parameter. Print has the following parameters: True, 1; false, and 0.

3. Differences:

The echo command is the same as the print command, and there is no difference;

Echo function and print
Functions are different;

Print () has a returned value. If the execution fails (such as disconnection), flase is returned;

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.

Use the <flag on the same page to tag a pair. Two or more tags with the same name are not allowed on the same page. The end of the paired tag name should be a separate line, and no output characters (such as invisible but existing characters such as spaces) are allowed ).

For example, the following writing format:

Echo <EOT

EOT;

Print <EOT

EOT;

Its meaning is as follows:

<: Operator, which treats the content between custom delimiters as a string and processes the variables in it. EOT: Custom delimiters, which must end at the beginning of the line.

 

In addition, the sprintf () function has the same function as printf (), but it assigns the output to a string instead of directly assigning it to the browser. The format is as follows:

String sprintf (string format [, mixed arguments])

Example:

 
$ Cost=Sprintf("$ %. 2f", 43.2 );//Cost = $43.20

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.