PHP、Printf、Sprintf 函数 [英] PHP, Printf,Sprintf Functions

查看:41
本文介绍了PHP、Printf、Sprintf 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究printf,sprintf,但我没有理解几点,如果有人可以帮助我理解这些点,

I am studying about printf,sprintf and i didnt understand few points, if can some one please help me understand thos points,

此链接位于 PHP 手册:

有从一到六的解释:

我不明白的是:第一个和第二个(1(符号说明符),2(填充说明符)),如果有人可以帮我举个例子,我会非常感激.

What i didnt understand is: The First and The Second(1(sign specifier), 2(padding specifier)), if can some one please help me with example for thos i will be very thankful.

推荐答案

符号说明符强制一个符号,即使它是正的.所以,如果你有

The sign specifier forces a sign, even if it's positive. So, if you have

$x = 10;
$y = -10;
printf("%+d", $x);
printf("%+d", $y);

你会得到:

+10
-10

填充说明符添加左填充,以便输出始终采用一定数量的空格,这使您可以对齐一堆数字,这在生成带有总计等的报告时很有用.

The padding specifier adds left padding so that the output always takes a set number of spaces, which allows you to align a stack of numbers, useful when generating reports with totals, etc.

 $x = 1;
 $y = 10;
 $z = 100;
 printf("%3d\n", $x);
 printf("%3d\n", $y);
 printf("%3d\n", $z);

你会得到:

   1
  10
 100

如果在填充说明符前面加上零,字符串将被零填充而不是空格:

If you prefix the padding specifier with a zero, the strings will be zero padded instead of space padded:

 $x = 1;
 $y = 10;
 $z = 100;
 printf("%03d\n", $x);
 printf("%03d\n", $y);
 printf("%03d\n", $z);

给出:

 001
 010
 100

这篇关于PHP、Printf、Sprintf 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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