echo print print_r sprintf var_dump var_export

var_export

var_export

(PHP 4 >= 4.2.0, PHP 5)

var_export -- 输出或返回一个变量的字符串表示

描述

mixed var_export ( mixed expression [, bool return] )

此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码。

您可以通过将函数的第二个参数设置为 TRUE,从而返回变量的表示。

<pre>
<?php
$a
= array (1, 2, array ("a", "b", "c"
));
var_export ($a
);

/* 输出:
array (
  0 => 1,
  1 => 2,
  2 =>
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)
*/

$b = 3.1
;
$v = var_export($b, TRUE
);
echo
$v
;

/* 输出:
3.1
*/
?>
</pre>


var_dump

(PHP 3 >= 3.0.5, PHP 4, PHP 5)

var_dump -- 打印变量的相关信息

描述

void var_dump ( mixed expression [, mixed expression [, ...]] )

此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。

提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。

可以比较一下 var_dump()print_r()


print_r

(PHP 4, PHP 5)

print_r --  打印关于变量的易于理解的信息。

描述

bool print_r ( mixed expression [, bool return] )

注: 参数 return 是在 PHP 4.3.0 的时候加上的

print_r() 显示关于一个变量的易于理解的信息。如果给出的是 stringintegerfloat,将打印变量值本身。如果给出的是 array,将会按照一定格式显示键和元素。object 与数组类似。

记住,print_r() 将把数组的指针移到最后边。使用 reset() 可让指针回到开始处。

 

 

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

 

 

上边的代码将输出:

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

如果想捕捉 print_r() 的输出,可使用 return 参数。若此参数设为 TRUEprint_r() 将不打印结果(此为默认动作),而是返回其输出。

例子 1. return 参数示例

<?php
    $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 之前的版本中,如果给出的 arrayobject 包含了直接或间接指向自身的引用,print_r() 将永远继续下去。print_r($GLOBALS) 就是一个例子,因为 $GLOBALS 自身即是全局变量,其包含了指向自身的引用。

 


 

 

echo

(PHP 3, PHP 4, PHP 5)

echo -- Output one or more strings

Description

void echo ( string arg1 [, string ...] )

Outputs all parameters.

echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.


print

(PHP 3, PHP 4, PHP 5)

print -- Output a string

Description

int print ( string arg )

Outputs arg. Returns 1, always.

print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.

 


是命令,不能返回值。echo后面可以跟很多个参数,之间用分号隔开,如:
echo $myvar1;
echo 1,2,$myvar,"bold";

- print

是函数,可以返回一个值,只能有一个参数。


- printf

函数,把文字格式化以后输出,如:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);

- sprintf

跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。

- echo

是命令,不能返回值。echo后面可以跟很多个参数,之间用分号隔开,如:
echo $myvar1;
echo 1,2,$myvar,"bold";

- print

是函数,可以返回一个值,只能有一个参数。

- printf

函数,把文字格式化以后输出,如:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);

- sprintf

跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值