Current Location: Home> Latest Articles> How does the vfprintf function support formatted content output of multiple different data types? Practical examples

How does the vfprintf function support formatted content output of multiple different data types? Practical examples

gitbox 2025-05-29

What is vfprintf()?

vfprintf() is a function provided by PHP to write formatted strings into a specified file stream. Unlike fprintf() , vfprintf() receives an array of parameter lists. This makes it more flexible when dynamically splicing and processing multiple types of data.

Function definition:

 int vfprintf ( resource $handle , string $format , array $args )
  • $handle : File pointer resource, usually returned by fopen() .

  • $format : Format string.

  • $args : The array of data to be inserted in the formatted string.


Data types supported by format strings

In vfprintf() , we can use a series of format identifiers to support different types of output. Common ones include:

  • %s : string

  • %d : integer (decimal)

  • %f : floating point number

  • %x : hexadecimal (lowercase)

  • %X : Hexadecimal (caps)

  • %b : binary

  • %c : ASCII character

These identifiers can be combined with modifiers (such as precision, padding, etc.) to complete more complex output requirements.


Practical examples

Example 1: Write strings and integers

 <?php
$fp = fopen("log.txt", "a");
$name = "Alice";
$age = 30;
vfprintf($fp, "Name: %s, Age: %d\n", array($name, $age));
fclose($fp);
?>

This code writes the following to the log.txt file:

 Name: Alice, Age: 30

Example 2: Write a floating point number and keep two decimal places

 <?php
$fp = fopen("log.txt", "a");
$product = "Coffee";
$price = 19.456;
vfprintf($fp, "Product: %s, Price: \$%.2f\n", array($product, $price));
fclose($fp);
?>

Output:

 Product: Coffee, Price: $19.46

%.2f here means keeping two decimal places.


Example 3: Dynamically generate URLs and write files

 <?php
$fp = fopen("links.txt", "a");
$endpoint = "api/data";
$id = 42;
$url = "https://gitbox.net/%s?id=%d";
vfprintf($fp, $url."\n", array($endpoint, $id));
fclose($fp);
?>

The contents written to the file are:

 https://gitbox.net/api/data?id=42

Example 4: Mixed type writing log

 <?php
$fp = fopen("debug.log", "a");
$time = date("Y-m-d H:i:s");
$userId = 101;
$success = true;
$ip = "192.168.1.10";
vfprintf($fp, "[%s] User ID: %d, Success: %s, IP: %s\n", array(
    $time, $userId, $success ? "true" : "false", $ip
));
fclose($fp);
?>

The output is similar:

 [2025-05-29 15:30:00] User ID: 101, Success: true, IP: 192.168.1.10

Tips and precautions

  1. Security : Avoid user input being spliced ​​directly into format strings, preventing format injection attacks.

  2. The order of the array must be accurate : the parameter array must correspond to the format string one by one, and the order cannot be wrong.

  3. File permissions : Make sure that the file written has write permission, otherwise it will cause fopen() or vfprintf() to fail.

  4. Multilingual adaptation : When formatting the output, you can pre-process it with sprintf() and vprintf() .