Home >Backend Development >PHP Tutorial >Differences in the use of php echo, print, print_r, sprintf, var_dump, var_expor_PHP tutorial
/*******echo********/
echo— 输出一个或多个字符串
描述
echo ( string arg1 [, string ...] )
echo()实际上不是一个函数(它是一个语言结构),所以您不需要使用括号。echo()的(不同于其他一些语言构造)并不像一个功能,所以它不能总是在函数中使用。此外,如果你想传递多个参数的echo(),参数必须不被括在括号内。
echo()是命令,不能返回值。echo后面可以跟很多个参数,之间用分号隔开,如:
echo $myvar1;
echo 1,2,$myvar,”bold”;
/*******print********/
print— 输出一个或多个字符串
描述
int print ( string arg )
print()是实际上没有一个真正的函数(它是一个语言结构),所以你并不需要使用它的参数列表的括号。
可以返回一个值,只能有一个参数
/*******print_r()********/
print_r
(PHP 4, PHP 5)
print_r – 打印关于变量的易于理解的信息。
描述
bool print_r ( mixed expression [, bool return] )
注: 参数 return 是在 PHP 4.3.0 的时候加上的
print_r() 显示关于一个变量的易于理解的信息。如果给出的是 string、integer 或 float,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素。object 与数组类似。
记住,print_r() 将把数组的指针移到最后边。使用 reset() 可让指针回到开始处。
<br><?php<BR> $a = array (‘a' => ‘apple', ‘b' => ‘banana', ‘c' => array (‘x','y','z'));<br> print_r ($a);<br>?><br>
<br>Array<br>(<br> [a] => apple<br> [b] => banana<br> [c] => Array<br> (<br> [0] => x<br> [1] => y<br> [2] => z<br> )<br>)<br>
/*******var_export()*******/
var_export
(PHP 4 >= 4.2.0, PHP 5)
var_export — output or Returns a string representation of a variable
Description
mixed var_export ( mixed expression [, bool return] )
This function returns structural information about the variable passed to this function, it and var_dump() is similar, except that the representation returned is legal PHP code.
var_export must return legal PHP code. In other words, the code returned by var_export can be directly assigned to a variable as PHP code. And this variable will get the same type of value as var_export
However, when the variable type is resource, it cannot be simply copied. Therefore, when the variable of var_export is of resource type, var_export will return NULL
<br><?php<BR>$a = array (1, 2, array ("a", "b", "c"));<BR>var_export ($a);<BR>/* Output: <BR>array (<BR> 0 => 1 ,<br> 1 => 2,<br> 2 =><br> array (<br> 0 => 'a',<br> 1 => 'b',<br> 2 => ; 'c',<br> ),<br>)<br>*/<br>$b = 3.1;<br>$v = var_export($b, TRUE);<br>echo $v;<br>/* Output: <br>3.1<br>*/<br>?><br>