The difference between Echo and print in PHP, phpechoprint_php tutorial

Source: Internet
Author: User
Tags echo command

The difference between Echo and print in PHP, Phpechoprint


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. '
';//echo can display print $a with a comma-delimited string variable. $b. '
';//and print cannot use commas, only separated by Dot,//print $a, $b. '
';//Use the comma-times error 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  <<< EOT EOT; print  

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.


Echo () print () Print-r () different in PHP

Echo
Multiple values can be output at one time, and multiple values are separated by commas. Echo is a language structure (language construct), not a real function and therefore cannot be used as part of an expression.
Print ()
The function print () prints a value (its arguments) and returns True if the string is successfully displayed, otherwise false.
Print_r ()
You can simply print the string and the number, and the array is displayed with the enclosed key and the list of merits, starting with an array. However, the result of Print_r () output Boolean and null is meaningless because it is all printed "\ n". Therefore, using the Var_dump () function is more suitable for debugging.

What is the difference between echo (), print (), and Print_r () in PHP?

There are four ways to output a string. Echo

Print ()

printf ()

Print_r ()
Echo
Multiple values can be output at one time, and multiple values are separated by commas. Echo is a language structure (language construct), not a real function and therefore cannot be used as part of an expression.

Grammatically correct: Echo "Hello", "World";
Syntax error: Echo ("Hello", "World");
Print ()
The function print () prints a value (its arguments) and returns True if the string is successfully displayed, otherwise false. For example, if (!print ("Hello, World")) {

Die ("is not a listening to me");

}
printf ()
printf () originates from printf () in the C language. The function outputs a formatted string.
Syntax: printf (format,arg1,arg2,arg++)
format Specifies the string and how the variable is formatted;
Arg1, arg2, + + etc parameters will be inserted into the main string percent percent (%) Symbol. This function is executed step-by. In the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.
Example:? php

$str = "Hello";

$number = 123;

printf ("%s World". Day number%u ", $STR, $number);

?>
#Results ======
Hello World. Day Number 123

If the% symbol is more than the arg parameter, you must use a placeholder. After the placeholder is inserted in the% symbol, it consists of a number and a "\$". See Example 3.
Example:? php

$number = 123;

printf ("With 2 decimals:%1\$.2FBR/>with no decimals:%1\ $u", $number);

?>
#Result
With 2 decimals:123.00
With no decimals:123

Print_r () and Var_dump ()
Print_r () can simply print the string and the number, and the array is displayed with the enclosed key and the list of merit, beginning with an array. For example, $a = Array (' name ' = ' Fred ', ' age ' = ' + ', ' wife ' = ' Wilma ');

Print_r ($a);
Output:array

{

[Name] + Fred

[Age] = 15

[Wife] = Wilma

}
objects are the same. For example, class P {

var $name = ' nat ';

// ...

}

$p = new P;

Print_r ($p);
Output:object

{

[Name] = NAT

}
But the result of Print_r () output Boolean and null is meaningless, because all are printed &quo ... Remaining full text >>

http://www.bkjia.com/PHPjc/871103.html www.bkjia.com true http://www.bkjia.com/PHPjc/871103.html techarticle the difference between echo and print in PHP, Phpechoprint in general, the dynamic output of HTML content in PHP is achieved through print and ECHO statements, and in practice, print and echo work ...

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