Home > php教程 > php手册 > The difference between echo(), print(), print_r(), var_dump() in php

The difference between echo(), print(), print_r(), var_dump() in php

WBOY
Release: 2016-10-19 10:19:35
Original
1191 people have browsed it

echo() function: Output one or more strings. In fact, it is not a function, so there is no need to use parentheses on it, just use echo. However, if you wish to pass more than one argument to echo(), using parentheses will generate a parsing error. The echo() function is slightly faster than print(). When echo outputs multiple strings, separate them with commas.

For example 1: (Write the value of the string variable $str into the input)

$str="hello world!";

echo $str;

?>

Run result: hello world!

Example 2: (echo outputs multiple strings)

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

print() function: Output one or more strings. Like echo, it is not actually a function. Print has a return value, but echo does not. When its execution fails, it returns false, and if it succeeds, it returns true. The speed is slightly slower than echo. Only the values ​​of simple type variables can be printed out, such as int and string.

For example 1: (Write the value of the string variable $str to the output)

$str="hello world!";

print $str;

?>

print_r() function: can print out the value of complex type variables. Print_r() can be used to print out the entire array content and structure, displaying keys and elements in a certain format. In fact, it is not only used for printing, but for printing easy-to-understand information about variables.

For example 1: (Print array $age)

$age=array(18,20,24);

print_r($age);

?>

Run result: Array ( [0] => 18 [1] => 20 [2] => 24 )

var_dump() function: determines the type and length of a variable, and outputs the value of the variable. If the variable has a value, the value of the variable is output, and the data type is returned. This function displays structural information about one or more expressions, including the expression's type and value. Arrays will expand values ​​recursively, showing their structure through indentation.

Example 1:

$age=array(18,20,24);

var_dump($age);

?>

Run result: array(3) { [0]=> int(18) [1]=> int(20) [2]=> int(24) }

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template