PHP,Printf,Sprintf函数

Bla*_*ext 2 php printf function

我正在学习有关printf,并sprintf和我不明白几点.有人可以帮我理解以下format说明的解释sprintf():

  • 一个可选的对齐说明符,表示结果是左对齐还是右对齐.默认是右对齐; a - 这里的字符将使其左对齐.

  • 一个可选数字,一个宽度说明符,表示此转换应产生的字符数(最小值).

Mar*_*c B 7

宽度说明符:

given:    printf('|%5d|', 1);
prints:   |    1|
           ^^^^^-- 4 spaces + 1 char = width of 5
Run Code Online (Sandbox Code Playgroud)

对准:

given:    printf('|%-5d|', 1);
prints    |1    |
           ^^^^^-- 1 char + 4 right-justified spaces = width of 5.
Run Code Online (Sandbox Code Playgroud)

  • 我们从中学到了什么?就试一试吧. (3认同)