Home >>PHP String Functions >PHP fprintf() Function

PHP fprintf() Function

PHP frintf() Function

PHP fprintf() function is used to write a string in a defined format to any specific Output stream like any file or any database. In this fprintf() function we can pass many number of variables as we need in the string.

The very first parameter is $stream which contains the stream value that defines where the string is to be write or return as output.

$format defines the format in which the string is to be sent or inserted in the database or file.

$arg1 is the argument which is to be inserted at first. $stream , $format, and $arg1 are required parameters where as other parameters are optional. $arg2 is the argument which is to be inserted at second, and $arg++ are the arguments which are to be inserted next.

Syntax :

fprintf($stream,$format,$arg1,$arg2,$arg++);

Here is an example of fprintf() function in PHP:

<html>
<body>

<?php
$age = 21;
$name = "Abhi";
$file = fopen("file.txt","w");
echo fprintf($file,"%s is %u years old.",$name,$age);
?>

</body>
</html>
Output:
Abhi is 21 years old.

The above code will write the string in the format "Abhi is 21 years old". In the given "file.txt" file.

There are many different possible format values, some of which are:-

<html>
<body>

<?php
$num1 = 123456789;
$num2 = -123456789;

printf("%%b = %b <br>",$num1); // Binary number
printf("%%d = %d <br>",$num1); // Signed decimal number
printf("%%e = %e <br>",$num1); // Scientific notation (lowercase)
printf("%%E = %E <br>",$num1); // Scientific notation (uppercase
printf("%%u = %u <br>",$num2); // Unsigned decimal number (negative)
printf("%%f = %f <br>",$num1); // Floating-point number (local settings aware)
printf("%%F = %F <br>",$num1); // Floating-point number (not local sett aware)
printf("%%g = %g <br>",$num1); // Shorter of %e and %f
printf("%%G = %G <br>",$num1); // Shorter of %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)

?>

</body>
</html>
Output:
%b = 111010110111100110100010101
%d = 123456789
%e = 1.234568e+8
%E = 1.234568E+8
%u = 18446744073586094827
%f = 123456789.000000
%F = 123456789.000000
%g = 1.23457e+8

Note : The format value "%%" returns a percent sign


PHP String Functions PHP addcslashes() Function PHP addslashes() Function PHP bin2hex() Function PHP chop() Function PHP chr() Function PHP chunk_split() Function PHP convert_cyr_string() Function PHP convert_uudecode() Function PHP convert_uuencode() Function PHP count_chars() Function PHP crc32() Function PHP crypt() Function PHP echo() Function PHP empty() function PHP explode() Function PHP strcmp() Function PHP fprintf() Function PHP strcoll() Function PHP get_html_translation_table() Function PHP strcspn() Function PHP hebrev() Function PHP strip_tags() Function PHP hebrevc() Function PHP hex2bin() Function PHP html_entity_decode() Function PHP htmlentities() Function PHP htmlspecialchars() Function PHP htmlspecialchars_decode() Function PHP implode() Function PHP join() Function PHP lcfirst() Function PHP levenshtein() Function PHP localeconv() Function PHP ltrim() Function PHP md5() Function PHP md5_file() Function PHP metaphone() Function PHP money_format() Function PHP nl_langinfo() Function PHP nl2br() Function PHP number_format() Function PHP ord() Function PHP parse_str() Function PHP print() Function PHP printf() Function PHP quoted_printable_decode() Function PHP quoted_printable_encode() Function PHP quotemeta() Function PHP rtrim() Function PHP setlocale() Function PHP sha1() Function PHP sha1_file() Function PHP similar_text() Function PHP soundex() Function PHP sprintf() Function PHP sscanf() Function PHP str_getcsv() Function PHP str_ireplace() Function PHP str_pad() Function PHP str_repeat() Function PHP str_replace() Function PHP str_rot13() Function PHP str_shuffle() Function PHP str_split() Function PHP str_word_count() Function PHP strcasecmp() Function PHP strchr() Function
No Sidebar ads