为什么在PHP中使用sprintf函数? [英] Why use sprintf function in PHP?

查看:62
本文介绍了为什么在PHP中使用sprintf函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习有关PHP函数sprintf()的更多信息,但由于我仍然感到困惑,php.net并没有太大帮助,为什么要使用它?

I am trying to learn more about the PHP function sprintf() but php.net did not help me much as I am still confused, why would you want to use it?

在下面查看我的示例.

为什么要使用它:

$output = sprintf("Here is the result: %s for this date %s", $result, $date);

当这样做相同并且更容易编写IMO时:

When this does the same and is easier to write IMO:

$output = 'Here is the result: ' .$result. ' for this date ' .$date;

我在这里想念东西吗?

推荐答案

sprintf具有原始printf的所有格式化功能,这意味着您不仅可以在字符串中插入变量值,还可以做更多的事情.

sprintf has all the formatting capabilities of the original printf which means you can do much more than just inserting variable values in strings.

例如,指定数字格式(十六进制,十进制,八进制),小数位数,填充等. Google for printf,您会发现很多例子. 关于printf的维基百科文章应该使您入门.

For instance, specify number format (hex, decimal, octal), number of decimals, padding and more. Google for printf and you'll find plenty of examples. The wikipedia article on printf should get you started.

这篇关于为什么在PHP中使用sprintf函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆