Difference between echo and print in PHP, phpechoprint_PHP tutorial

Source: Internet
Author: User
The difference between echo and print in PHP is phpechoprint. The difference between echo and print in PHP. In general, PHP dynamically outputs HTML content through print and echo statements. in actual use, difference between echo and print in PHP: phpechoprint

In general, PHP dynamically outputs HTML content through print and echo statements. in actual use, print and echo functions are almost identical. In this case, you can use either of them. However, there is also 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 need parentheses, so the echo function is more like a statement than a function.
Echo and print are not functions, but language structures, so parentheses are not required.

Their differences are:

(1) echo can output multiple strings, as shown below:

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

If you need to add parentheses, be sure to write echo ('A', 'B', 'C'); it is incorrect and should be written:

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

It does not act like a function, so it cannot be used in the context of the function.
(2) print can only output one string, which can behave like a function. for example, you can use the following:

$ret = print 'Hello World';

It can be used in more complex expressions.
In addition, echo is relatively efficient!

Take a look at the following code:

<? Php $ a = 'hello'; $ B = 'php world! '; Echo $ a, $ B .'
'; // Echo can use commas to separate string variables to display print $ a. $ B .'
'; // And print cannot use commas. they can only be separated by dots. // print $ a, $ B .'
'; // If you use a comma, an error is returned.?>

Analysis Summary:

The echo command is the same as the print command.
The echo function differs from the print function.
Echo () has no return value, which is the same as the echo command.
Print () has a return value, success, return 1, false, return 0.
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.

Shape:

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

The format, meaning:

<Operator, which treats the content between custom delimiters as strings and processes the variables
EOT user-defined delimiters. the end must be at the beginning of the line.

I believe this article provides some reference value for you to better master PHP programming.


Echo () print-r () is different in php.

Echo
Multiple values can be output at a time. multiple values are separated by commas. Echo is a language construct rather than a real function. Therefore, it cannot be used as part of an expression.
Print ()
The print () function prints a value (its parameter). If the string is successfully displayed, true is returned. otherwise, false is returned.
Print_r ()
Strings and numbers can be simply printed out, while arrays are displayed in the form of an enclosed key and a list of values, starting with Array. But print_r () outputs Boolean values and NULL results are meaningless, because both print "\ n ". Therefore, using the var_dump () function is more suitable for debugging.

What are the differences between echo (), print (), and print_r () in PHP?

Four methods can output strings. Echo

Print ()

Printf ()

Print_r ()
Echo
Multiple values can be output at a time. multiple values are separated by commas. Echo is a language construct rather than a real function. Therefore, it cannot be used as part of an expression.

Correct syntax: echo "Hello", "World ";
Syntax error: echo ("Hello", "World ");
Print ()
The print () function prints a value (its parameter). If the string is successfully displayed, true is returned. otherwise, false is returned. For example, if (! Print ("Hello, World ")){

Die ("you are not listening to me ");

}
Printf ()
Printf () is derived from printf () in C language (). This function outputs formatted strings.
Syntax: printf (format, arg1, arg2, arg ++)
Format specifies the string and how to format the variable;
Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, 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 greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "\ $. 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 print strings and numbers, while the Array is displayed in the form of an enclosed key and a list of values, and starts with Array. For example, $ a = array ('name' => 'Fred ', 'age' => '15', 'wife' => 'wilm ');

Print_r ($ );
Output: Array

{

[Name] => Fred

[Age] => 15

[Wife] => Wilma

}
The same is true for objects. For example, class P {

Var $ name = 'Nat ';

//...

}

$ P = new P;

Print_r ($ p );
Output: Object

{

[Name] => nat

}
But print_r () outputs the boolean value and the NULL result are meaningless, because both Print & quo ...... the remaining full text>

In general, PHP outputs HTML content dynamically through print and echo statements. in actual use, the functions of print and echo...

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.