PHP fprintf() Function

In this chapter you will learn:

  1. Definition for PHP fprintf() Function
  2. Syntax for PHP fprintf() Function
  3. Parameter for PHP fprintf() Function
  4. Additional format
  5. Return for PHP fprintf() Function
  6. Related functions
  7. Example - Write some text to a text file named "test.txt"
  8. Example - Use of placeholders
  9. Example - Using printf() to demonstrate all possible format values

Definition

The fprintf() function writes a formatted string to a specified output stream.

Syntax

PHP fprintf() Function has the following syntax.

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

Parameter

ParameterIs RequiredDescription
streamRequired.Where to write/output the string
formatRequired.String and how to format the variables in it.
arg1Required.The argument to be inserted at the first %-sign in the format string
arg2Optional.The argument to be inserted at the second %-sign in the format string
arg++Optional.The argument to be inserted at the third, fourth, etc. %-sign in the format string

Possible format values:

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format

Additional format values. These are placed between the % and the letter (example %.2f):

FormatMeaning
+Forces both + and - in front of numbers. By default, only negative numbers are marked
'Sets what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s this uses "x" as padding
-Left-justifies the variable value
[0-9]Sets the minimum width held of to the variable value
.[0-9]Sets the number of decimal digits or maximum string length

Multiple additional format values must be in the same order as above.

Return

PHP fprintf() Function returns the length of the written string.

printf(), sprintf(), vprintf(), vsprintf() and vfprintf()

Example 1

Write some text to a text file named "test.txt":


<?php//j  ava2 s  .  co m
$number = 1;
$str = "PHP";
$file = fopen("test.txt","w");
echo fprintf($file,"There are %u million developers using %s.",$number,$str);
echo fprintf($file,"%f",$number);


?>

The code above generates the following result.

Example 2

Use of placeholders:


<?php/* java2  s. c  o  m*/
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",$number);
?>

Example 3

Using printf() to demonstrate all possible format values:


<?php//  j a va2 s .  co m
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

// Note: The format value "%%" returns a percent sign
printf("%%b = %b \n",$num1); // Binary number
printf("%%c = %c \n",$char); // The ASCII Character
printf("%%d = %d \n",$num1); // Signed decimal number
printf("%%d = %d \n",$num2); // Signed decimal number
printf("%%e = %e \n",$num1); // Scientific notation (lowercase)
printf("%%E = %E \n",$num1); // Scientific notation (uppercase)
printf("%%u = %u \n",$num1); // Unsigned decimal number (positive)
printf("%%u = %u \n",$num2); // Unsigned decimal number (negative)
printf("%%f = %f \n",$num1); // Floating-point number (local settings aware)
printf("%%F = %F \n",$num1); // Floating-point number (not local settings aware)
printf("%%g = %g \n",$num1); // Shorter of %e and %f
printf("%%G = %G \n",$num1); // Shorter of %E and %f
printf("%%o = %o \n",$num1); // Octal number
printf("%%s = %s \n",$num1); // String
printf("%%x = %x \n",$num1); // Hexadecimal number (lowercase)
printf("%%X = %X \n",$num1); // Hexadecimal number (uppercase)
printf("%%+d = %+d \n",$num1); // Sign specifier (positive)
printf("%%+d = %+d \n",$num2); // Sign specifier (negative)
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP fputcsv() Function
  2. Syntax for PHP fputcsv() Function
  3. Parameter for PHP fputcsv() Function
  4. Return for PHP fputcsv() Function
  5. Example - formats a line as CSV and writes it to an open file
Home » PHP Tutorial » PHP File Functions
PHP basename() function
PHP chdir() function
PHP chgrp() Function
PHP chmod() Function
PHP chown() Function
PHP chroot() Function
PHP clearstatcache() Function
PHP closedir() Function
PHP copy() Function
PHP delete() function
PHP dir() Function
PHP dirname() Function
PHP disk_free_space() Function
PHP disk_total_space() Function
PHP diskfreespace() Function
PHP fclose() Function
PHP feof() Function
PHP fflush() Function
PHP fgetc() Function
PHP fgets() Function
PHP fgetss() Function
PHP file() Function
PHP file_exists() Function
PHP file_get_contents() Function
PHP file_put_contents() Function
PHP fileatime() Function
PHP filectime() Function
PHP filegroup() Function
PHP fileinode() Function
PHP filemtime() Function
PHP fileowner() Function
PHP fileperms() Function
PHP filesize() Function
PHP filetype() Function
PHP flock() Function
PHP fnmatch() Function
PHP fopen() Function
PHP fpassthru() Function
PHP fprintf() Function
PHP fputcsv() Function
PHP fputs() Function
PHP fread() Function
PHP fscanf() Function
PHP fseek() Function
PHP fstat() Function
PHP ftell() Function
PHP ftruncate() Function
PHP fwrite() Function
PHP getcwd() function
PHP glob() Function
PHP is_dir() Function
PHP is_executable() Function
PHP is_file() Function
PHP is_link() Function
PHP is_readable() Function
PHP is_uploaded_file() Function
PHP is_writable() Function
PHP link() Function
PHP linkinfo() Function
PHP lstat() Function
PHP md5_file() Function
PHP mkdir() Function
PHP move_uploaded_file() Function
PHP opendir() Function
PHP parse_ini_file() Function
PHP pathinfo() Function
PHP pclose() Function
PHP popen() Function
PHP readdir() Function
PHP readfile() Function
PHP readlink() Function
PHP realpath() Function
PHP rename() Function
PHP rewind() Function
PHP rewinddir() Function
PHP rmdir() Function
PHP scandir() Function
PHP set_file_buffer() Function
PHP sha1_file() Function
PHP stat() Function
PHP symlink() Function
PHP tempnam() Function
PHP tmpfile() Function
PHP touch() Function
PHP umask() Function
PHP unlink() Function
PHP vfprintf() Function