Differences between echo and print and print_r functions _ PHP Tutorial

Source: Internet
Author: User
Differences between echo and print functions and print_r functions. This article introduces the differences between the three output functions echo and print and print_r. next we will introduce the usage speed. 1. differences between echo and print: echo and print functions are basically the same in PHP (This article introduces the differences between echo and print functions and print_r. next we will introduce the usage speed.

1. differences between echo and print

The echo and print functions in PHP are basically the same (output), but there are still slight differences between the two. No return value after echo output, but print has a return value. if the execution fails, flase is returned. Therefore, it can be used as a common function. for example, after the following code is executed, the value of $ r is 1.

$ R = print "Hello World ";

This means that print is available in some complex expressions, but echo is not. However, because the echo statement does not require any value to be returned, the running efficiency of the echo statement in the code is slightly faster than that of the print statement.


Echo does not return values; print has a return value, and print always returns 1.

Expression
Print can be used for complex expressions, but echo cannot. For example, print can be used as follows:

The code is as follows:

Parameters
Echo can have multiple parameters, while print can have only one parameter.

If multiple parameters exist in echo, separate them with commas (,). you do not need to add parentheses for each parameter. the correct syntax is as follows:

The code is as follows:
Echo "good", "for", "you ";

Note: If echo has multiple parameters, it is incorrect to enclose all parameters with only one parentheses. The following statement is incorrect:

The code is as follows:
Echo ("good", "for", "you ");

Print can have only one parameter, for example:

The code is as follows:

Print ("good for you ");
Print "good for you ";

Echo and print are both output strings. The main difference between echo and print is that echo is faster than print because echo has no return value.


Print_r () function, used only to output arrays.

In php, the content of the arrays output by the print_r function is not arranged. To make the output look better. For example, arrays have multiple layers. To list segments, we can write as follows:

Example #1 print_r () example

The code is as follows:


$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>

The above example will output:


Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)

Bytes. 1. differences between echo and print: echo and print functions in PHP are basically the same (...

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.