In PHP programming, the vfprintf function is a convenient way to output formatted strings to a file. Its functionality is similar to fprintf but allows multiple parameters to be passed through an array, enhancing flexibility. This article will introduce in detail how to use the vfprintf function and demonstrate its operation steps through specific examples.
The function of the vfprintf function is to write the formatted string to the specified file resource. Its function prototype is as follows:
int vfprintf(resource $stream, string $format, array $args)
$stream : The file resource handle, must be an opened file.
$format : Format string, similar to the format definition in printf .
$args : an array containing the values to be formatted.
The function returns the number of characters written to the file, and if it fails, it returns false .
The following code demonstrates how to write formatted content to a file:
<?php
// Open the file in write mode
$file = fopen("output.txt", "w");
if ($file === false) {
die("Unable to open the file!");
}
// Define formatted strings and corresponding parameter arrays
$format = "Name:%s,age:%d,Mail:%s\n";
$args = ["Zhang San", 28, "[email protected]"];
// use vfprintf Write formatted string to file
vfprintf($file, $format, $args);
// Close file resources
fclose($file);
echo "Data writing successfully!";
?>
illustrate :
In the formatted string, %s represents the string placeholder, and %d represents the integer placeholder.
Elements in the $args array replace placeholders in the formatted string in order.
The email domain name here uses gitbox.net , which meets your requirements.
Open the file <br> Use fopen to open the target file and select the appropriate read and write mode (such as "w" means writing, which will overwrite the original content, and "a" means append).
Prepare to format string <br> Design the format string according to the requirements and determine the content and type that needs to be output.
Build parameter array <br> Save all variables that need to be output into the array in order.
Call vfprintf
Pass in file resources, format strings and parameter arrays to implement formatted writing.
Close the file <br> Use fclose to release file resources to avoid resource leakage.
<?php
$file = fopen("users.txt", "w");
if (!$file) {
exit("Failed to open the file!");
}
$format = "user:%s,Fraction:%d,website:%s\n";
$users = [
["Li Si", 95, "[email protected]"],
["Wang Wu", 82, "[email protected]"],
["Zhao Liu", 77, "[email protected]"],
];
foreach ($users as $user) {
vfprintf($file, $format, $user);
}
fclose($file);
echo "Multi-line data writing is completed。";
?>
This example shows how to write multiple formatted records to a file in a loop.
The file must be opened successfully before vfprintf can be called. Otherwise, an error will be caused.
The length and type of the format string and parameter array should match, otherwise the output will error.
Properly handle exceptions and errors in file operations to ensure the robustness of the program.