PHP vfprintf() Function

In this chapter you will learn:

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

Definition

The vfprintf() function writes a formatted string to a specified output stream (file or database).

Unlike fprintf(), the arguments in vfprintf(), are placed in an array.

Syntax

PHP vfprintf() Function has the following syntax.

vfprintf(stream,format,argarray)

Parameter

This function takes a variable number of parameters: the first parameter is a format string, followed by zero or other parameters of various types.

The format strings for sprintf() is listed as follows.

FormatMeaning
%% A literal percent character; no matching parameter is required
%b Parameter is an integer; output it as binary
%c Parameter is an integer; output it as a character with that ASCII value
%d Parameter is a positive integer; output it as decimal
%e Scientific notation using a lowercase (e.g. 1.2e+2)
%E Scientific notation using a uppercase (e.g. 1.2E+2)
%f Parameter is a float; express it as a float
%F Floating-point number (not local settings aware)
%g shorter of %e and %f
%G shorter of %E and %f
%o Parameter is an integer; output it as octal
%s Parameter is a string; output it as a string
%uUnsigned decimal number (equal to or greather than zero)
%x Parameter is an integer; output it as hexadecimal with lowercase letters
%X Parameter is an integer; output it as hexadecimal with uppercase letters

Additional format

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

Additional formatMeaning
+Forces both + and - in front of numbers. By default, only negative numbers are marked
'What to use as padding. Default is space. Must be used together with the width specifier. Example: %'q20s (this uses "q" as padding)
-Left-justifies the variable value
[0-9]Minimum width held of to the variable value
.[0-9]Number of decimal digits or maximum string length

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

Return

Returns the length of the written string

Example 1

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


<?php/*from   j ava2s  .c o  m*/
$number = 1;
$str = "PHP";
$file = fopen("test.txt","w");
echo vfprintf($file,"There are %u million %s developers.",array($number,$str));
?>

Example 2

Write some text to a file:


<?php// ja v  a2 s  .c om
$num1 = 123;
$num2 = 456;
$file = fopen("test.txt","w");
vfprintf($file,"%f%f",array($num1,$num2));
?>

Example 3

Use of placeholders:


<?php/*from j a v a 2  s  .  c  om*/
$number = 123;
$file = fopen("test.txt","w");
vfprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",array($number));
?>

Example

Using printf() to demonstrate all possible format values:


<?php/*j  av a  2 s . c o  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)
?>

Next chapter...

What you will learn in the next chapter:

  1. What is PHP imagearc() function
  2. Syntax for PHP imagearc() function
  3. Parameter for PHP imagearc() function
  4. Return value for PHP imagearc() function
  5. Example - PHP imagearc() function
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