Home >Backend Development >PHP Tutorial >Differences in the use of php echo, print, print_r, sprintf, var_dump, var_expor_PHP tutorial

Differences in the use of php echo, print, print_r, sprintf, var_dump, var_expor_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-21 15:05:441056browse

/*******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>

如果想捕捉 print_r() 的输出,可使用 return 参数。若此参数设为 TRUE,print_r() 将不打印结果(此为默认动作),而是返回其输出。
例子 1. return 参数示例
$b = array (‘m' => ‘monkey', ‘foo' => ‘bar', ‘x' => array (‘x', ‘y', ‘z'));
    $results = print_r ($b, true); //$results 包含了 print_r 的输出结果
?>

注: 如果想在 PHP 4.3.0 之前的版本中捕捉 print_r() 的输出,可使用输出控制函数。
注: 在 PHP 4.0.4 之前的版本中,如果给出的 array 或 object 包含了直接或间接指向自身的引用,print_r() 将永远继续下去。print_r($GLOBALS) 就是一个例子,因为 $GLOBALS 自身即是全局变量,其包含了指向自身的引用。
 /*******sprintf()********/

/*******var_dump()********/
var_dump
(PHP 3 >= 3.0.5, PHP 4, PHP 5)
var_dump — 打印变量的相关信息
描述
void var_dump ( mixed expression [, mixed expression [, ...]] )
此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。
提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。

/*******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

Copy code The code is as follows:

$res = fopen('status.html', 'r');
var_dump($res); //resource(2) of type (stream)
var_export($res); //NULL

You can do this by setting the second parameter of the function to TRUE, thereby returning the representation of the variable.
Copy code The code is as follows:

<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 =&gt ; '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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327679.htmlTechArticle/*******echo********/ echo—Output one or more string descriptions echo ( string arg1 [, string ...] ) echo() isn't actually a function (it's a language construct), so you...
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