php中print、printf、sprintf的区别?

返回结果不同:

print —— 输出字符串
printf —— 返回值为输出字符串的长度,按照format格式参数直接输出字符串
sprintf —— 返回值为按照format格式参数格式化的字符串,不直接输出结果

说明:

int print ( string $arg )
参数:arg (需要输出的字符串)
返回:整形1
输出:字符串

print 实际上不是函数(而是语言结构),所以可以不用圆括号包围参数列表。
和 echo的区别仅是:print仅支持一个参数,并总是返回1。

int printf ( string format[,mixed f o r m a t [ , m i x e d args [, mixed $… ]] )
参数: format(描述信息) args (需要格式化的参数)
返回 :输出字符串的长度
输出:输出根据format参数格式化的字符串

实例1$num = 2.12; 
printf("%.1f",$num); 
prints: 
2.1 

实例2$goodevil = array('good', 'evil'); 
printf_array('There is a difference between %s and %s', $goodevil); 
?> 
and it will print: 
There is a difference between good and evil

string sprintf ( string format[,mixed f o r m a t [ , m i x e d args [, mixed $… ]] )
参数:format(格式化描述信息) args (需要格式化的参数)
返回:返回值为根据format格式化的字符串
输出:无输出

实例及format参数说明参考网址:http://www.w3school.com.cn/php/func_string_sprintf.asp

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值