English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP String String Functions Manual
fprintf() function is used to write formatted strings to a stream
int fprintf ( resource $handle , string $format [, mixed $args [, mixed $... ] ] )
Used to write formatted strings to a stream
It returns the length of the string written
Serial number | Parameters 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:
Additional format values. Must be placed between % and the letter (for example %.2f):
Note:If multiple of the above format values are used, they must be used in the order listed above and cannot be rearranged. |
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 ‹/›