PHP fprintf() function

Example

Write text to the text file named "test.txt":

<?php
$number = 9;
$str = "Beijing";
$file = fopen("test.txt","w");
echo fprintf($file,"There are 쥕lion bicycles in %s.",$number,$str);
?>

Output of the above code:

40

The following text will be written to the file "test.txt":

There are 9 million bicycles in Beijing.

Definition and usage

The fprintf() function writes the formatted string to the specified output stream (e.g., file or database):

arg1,arg2,arg++ Parameters will be inserted into the percentage sign (%) at the main string. The function is executed step by step. At the first % symbol, insert arg1, insert arg2, and so on.

注释:If the % symbol is more than arg If there are more parameters, you must use placeholders. Placeholders are inserted after the % symbol, consisting of numbers and "\$". See example 2.

Related functions:

Syntax

fprintf(stream,format,arg1,arg2,arg++)
Parameters Description
stream Required. Specifies where to write/output the string:
format

Required. Specifies the string and how to format the variables within it:

Possible format values:

  • %% - Returns a percentage sign %:
  • %b - Binary number:
  • %c - Character corresponding to ASCII value:
  • %d - Decimal number with sign (negative, 0, positive):
  • %e - Uses lowercase scientific notation (e.g., 1.2e+2):
  • %E - Uses uppercase scientific notation (e.g., 1.2E+2):
  • 蒝ecimal number without sign (greater than or equal to 0):
  • %f - Floating-point number (local setting):
  • %F - Floating-point number (non-local setting):
  • %g - Shorter %e and %f:
  • %G - Shorter %E and %f:
  • %o - Octal number:
  • %s - String:
  • %x - Hexadecimal number (lowercase letters):
  • %X - Hexadecimal number (uppercase letters):

Additional format value. Must be placed between % and a letter (e.g., %.2f):

  • + (在数字前面加上 + 或 - 来定义数字的正负性。)
    默认地,只有负数才做标记,正数不做标记)
  • ' (规定使用什么作为填充,默认是空格。它必须与宽度指定器一起使用。)
    例如:%'x20s(使用 "x" 作为填充))
  • - (左调整变量值)
  • 。[0-9] (规定变量值的最小宽度)
  • 。[0-9] (规定小数位数或最大字符串长度)

注释:如果使用多个额外的格式值,则必须按照上面的顺序进行使用。

arg1 必需。规定插到 format 字符串中第一个 %% 符号处的参数。
arg2 可选。规定插到 format 字符串中第二个 %% 符号处的参数。
arg++ 可选。规定插到 format 字符串中第三、四等等 %% 符号处的参数。

技术细节

返回值: 返回被写字符串的长度。
PHP 版本: 5+

更多实例

例子 1

把文本写入文件中:

<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"%%f",$number);
?>

下面的文本将被写入到文件 "test.txt":

123.000000

例子 2

使用占位符:

<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"有两位小数:%1\$.2f
\n没有小数:%1\$u",$number);
?>

下面的文本将被写入到文件 "test.txt":

有两位小数:123.00
没有小数:123

例子 3

使用 printf() 来演示所有可能的格式值:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // ASCII 字符 50 是 2
// 注释:格式值 "%%" 返回百分号
printf("%%b = %%b <br>",$num1); // 二进制数
printf("%%c = %%c <br>",$char); // ASCII 字符
printf("%%d = %%d <br>",$num1); // 带符号的十进制数
printf("%%d = %%d <br>",$num2); // 带符号的十进制数
printf("%%e = %%e <br>",$num1); // 科学计数法(小写)
printf("%%E = %%E <br>",$num1); // 科学计数法(大写)
printf("%鑾u <br>",$num1); // Ongetekend decimaal (positief)
printf("%鑾u <br>",$num2); // Ongetekend decimaal (negatief)
printf("%%f = %f <br>",$num1); // Float (afhankelijk van lokale instellingen)
printf("%%F = %F <br>",$num1); // Float (niet afhankelijk van lokale instellingen)
printf("%%g = %g <br>",$num1); // Korter dan %e en %f
printf("%%G = %G <br>",$num1); // Korter dan %E en %f
printf("%%o = %o <br>",$num1); // Octaal
printf("%%s = %s <br>",$num1); // String
printf("%%x = %x <br>",$num1); // Hexadecimaal (kleine letters)
printf("%%X = %X <br>",$num1); // Hexadecimaal (hoofdletters)
printf("%%+d = %+d <br>",$num1); // Tekensymbool (positief)
printf("%%+d = %+d <br>",$num2); // Tekensymbool (negatief)
?>

Uitvoer voorbeeld