English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP fprintf() Function Usage and Example

   PHP String String Functions Manual

fprintf() function is used to write formatted strings to a stream

Syntax

int fprintf ( resource $handle , string $format [, mixed $args [, mixed $... ] ] )

Definition and usage

Used to write formatted strings to a stream

Return value

It returns the length of the string written

Parameter

Serial numberParameters and descriptions
1

stream

Where the stream is specified to be written

2

format

Specify a string and include information about how to format the string

Possible format values:

  • %% - Return a percentage sign %

  • %b - Binary number

  • %c - Character corresponding to ASCII value

  • %d - Decimal number with a sign (negative number, 0, positive number)

  • %e - Use lowercase scientific notation (for example 1.2e+2

  • %E - Use uppercase scientific notation (for example 1.2E+2

  • 㩵n - Decimal number without a 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 values. Must be placed between % and the letter (for example %.2f):

  • + (Add to the front of the number + or - to define the sign of the number. By default, only negative numbers are marked, and positive numbers are not marked)

  • ' (Specify what to use as padding, the default is space. It must be used with the width specifier. For example: %'x20s (Using "x" as padding))

  • - (Left adjust variable value)

  • [0-9Minimum width for specifying variable values

  • .[0]-9] (specify the number of decimal places or maximum string length)

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

Online example

Try the following examples, using fprintf to format a floating-point number and write some text to a file:

<?php
   $input = 123;
   $file = fopen("sample.txt","r");
   
   fprintf($file,"%f",$input);
?>
Test and see‹/›

Output result-

123.000000

Use printf() to demonstrate all possible format values:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // 5The ASCII character of 0 is2
//Note: The format value "%%" returns a percentage 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 (supported by local settings)
printf("%%F = %F <br> $num1); // Floating-point number (not supported by local settings)
printf("%%g = %g <br> $num1); // Smaller numbers %e and %f
printf("%%G = %G <br> $num1); // Smaller numbers %E and %f
printf("%%o = %o <br> $num1); // Octal number
printf("%%s = %s <br> $num1); // String
printf("%%x = %x <br> $num1); // Hexadecimal number (lowercase)
printf("%%X = %X <br> $num1); // Hexadecimal number (uppercase)
printf("%%+d = %+d <br> $num1); // Symbolic indicator (positive)
printf("%%+d = %+d <br> $num2); // Symbolic indicator (negative)
?>
Test and see ‹/›

PHP String String Functions Manual