PHP printf() 函数

实例

输出格式化的字符串:

<?php
$number = 9;
$str = "北京";
printf("在%s有 䍏自行车。",$str,$number);
?>

Esempio di esecuzione

定义和用法

printf() 函数输出格式化的字符串。

arg1arg2arg++ 参数将被插入到主字符串中的百分号(%)符号处。该函数是逐步执行的。在第一个 % 符号处,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。

Comment:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入到 % 符号之后,由数字和 "\$" 组成。请参见例子 2。

提示:相关函数:sprintf()、 vprintf()、 vsprintf()、 fprintf() 和 vfprintf()

语法

printf(format,arg1,arg2,arg++)
参数 描述
format

必需。规定字符串以及如何格式化其中的变量。

可能的格式值:

  • %% - 返回一个百分号 %
  • %b - 二进制数
  • %c - ASCII 值对应的字符
  • %d - 包含正负号的十进制数(负数、0、正数)
  • %e - 使用小写的科学计数法(例如 1.2e+2)
  • %E - 使用大写的科学计数法(例如 1.2E+2)
  • 퉦包含正负号的十进制数(大于等于 0)
  • %f - 浮点数(本地设置)
  • %F - 浮点数(非本地设置)
  • %g - 较短的 %e 和 %f
  • %G - 较短的 %E 和 %f
  • %o - 八进制数
  • %s - 字符串
  • %x - 十六进制数(小写字母)
  • %X - 十六进制数(大写字母)

附加的格式值。必需放置在 % 和字母之间(例如 %.2f):

  • +(在数字前面加上 + 或 - 来定义数字的正负性。默认地,只有负数做标记,正数不做标记)
  • (指定使用什么作为填充,默认是空格。它必须与宽度指定器一起使用。)
  • - (left aligns the variable value)
  • .[0-9] (specifies the minimum width of the variable value)
  • .[0-9] (specifies the number of decimal places or maximum string length)

Comment:If multiple of the above format values are used, they must be used in the order listed above and cannot be disrupted.

arg1 Required. Specifies the argument to be inserted format The parameter at the first % symbol in the string.
arg2 Required. Specifies the argument to be inserted format The parameter at the second % symbol in the string.
arg++ Optional. Specifies the argument to be inserted format The parameters at the third, fourth, etc. % symbols in the string.

Technical details

Return value: Returns the length of the output string.
PHP version: 4+

More examples

Example 1

Using format value %f:

<?php
$number = 123;
printf("%f",$number);
?>

Esempio di esecuzione

Example 2

Using placeholders:

<?php
$number = 123;
printf("With two decimal places:%1\$.2f<br>No decimal:%1\$u",$number);
?>

Esempio di esecuzione

Example 3

Demonstration of all possible format values:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // ASCII character 50 is 2
// Comment: The format value "%%" returns a percent sign
printf("%%b = %b <br>",$num1); // Binary number
printf("%%c = %c <br>",$char); // ASCII character
printf("%%d = %d <br>",$num1); // Signed decimal number
printf("%%d = %d <br>",$num2); // Signed decimal number
printf("%%e = %e <br>",$num1); // Scientific notation (lowercase)
printf("%%E = %E <br>",$num1); // Scientific notation (uppercase)
printf("%鑾u <br>",$num1); // Unsigned decimal number (positive)
printf("%鑾u <br>",$num2); // Unsigned decimal number (negative)
printf("%%f = %f <br>",$num1); // Floating-point number (respecting local settings)
printf("%%F = %F <br>",$num1); // Floating-point number (not respecting local settings)
printf("%%g = %g <br>",$num1); // Shorter than %e and %f
printf("%%G = %G <br>", $num1); // Più corto di %E e %f
printf("%%o = %o <br>", $num1); // Numero ottale
printf("%%s = %s <br>", $num1); // Stringa
printf("%%x = %x <br>", $num1); // Numero esadecimale (minuscolo)
printf("%%X = %X <br>", $num1); // Numero esadecimale (maiuscolo)
printf("%%+d = %+d <br>", $num1); // Simbolo di segno (positivo)
printf("%%+d = %+d <br>", $num2); // Simbolo di segno (negativo)
?>

Esempio di esecuzione

Esempio 4

Dimostrazione dei simboli di stringa:

<?php
$str1 = "Hello";
$str2 = "Hello world!";
printf("[%s]<br>", $str1);
printf("[%8s]<br>", $str1);
printf("[%-8s]<br>", $str1);
printf("[%08s]<br>", $str1);
printf("[%'*8s]<br>", $str1);
printf("[%8.8s]<br>", $str2);
?>

Esempio di esecuzione