Table of Contents

Syntax

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

(PHP 5)

$res A file handle resource returned by a call to fopen or similar.
$format An output format string just like the C printf function.
$args A variable number of arguments used as input to the formatting string.
RETURNS The length of the outputted string (once argument replacement has been done).

What it Does

Prints a formatted string to the file represented by the file handle argument ($res). Like the printf function in C, this fprintf function takes a format string and a variable number of arguments. Below is an example of some of the format specifiers available:

Format Specifiers

Format Meaning
%d decimal (integer) number
%f floating point number
%s string
%x hexadecimal number
%5d 5-digit decimal number
%7.3f floating point with 7 significant digits and 3 digits to the right of the decimal point

Example

// split the month abbreviations
if ($res = fopen('sample.out', 'w')) {
    $count = fprintf($res, "%s is %d years old", "sam", 12);
    echo 'Write ' . $count . ' characters to the file';
}
 
fprintf.txt · Last modified: Apr 18, 2008 - 2:00pm (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki