Home >Backend Development >PHP Problem >What is the difference between php printf and echo

What is the difference between php printf and echo

coldplay.xixi
coldplay.xixiOriginal
2020-08-29 13:52:452646browse

php The difference between printf and echo is: 1. echo can output multiple strings, while print can only output one string; 2. [echo()] has no return value, which is the same as the echo command, while [ print()] has a return value, returning 1 on success and 0 on failure.

What is the difference between php printf and echo

Related learning recommendations: php graphic tutorial

The difference between printf and echo is:

Dynamic output of HTML content in PHP is achieved through print and echo statements. In actual use,

print The functions of the two are almost identical to echo. It can be said that wherever one can be used, the other can also be used. However, there is still 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 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 necessary.

The difference is:

(1) echo can output multiple strings, like the following:

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

You must add parentheses, Note that it is wrong to write echo ('a','b','c');, it should be written as:

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

does not have function-like behavior, so it cannot be used in the context of a function

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

$ret = print 'Hello World';

, so it can be used in more complex expressions.

In addition, the efficiency of echo is relatively fast!

Code:

$a='hello ';$b='php world!';echo $a,$b.'
';//echo 可以用逗号分隔字符串变量来显示
print $a.$b.'
';//而print不能使用逗号,只能用点号分隔,
//print $a,$b.'
';//这里使用逗号时报错。
?>

Analysis summary:

The echo command is the same as the print command, there is no difference; echo There is a difference between function and print function.

  • echo() has no return value, the same as the echo command

  • print() has a return value, returns 1 if successful, returns false 0.

  • printf() is similar to sprintf(), both are formatted outputs. The difference is that the former outputs to the standard output, and the latter outputs to the variable

Forms like:

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

Its meaning:

  • #<<< operator will be defined by a custom delimiter The content between is treated as a string, and the variables between can be processed

  • EOTCustom delimiter, the end must be at the beginning of the line

Related learning recommendations:

php programming (video)

The above is the detailed content of What is the difference between php printf and echo. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn