The basic syntax of the vfprintf function is as follows:
vfprintf(resource $stream, string $format, array $args): int|false
$stream : file resource handle, obtained through functions such as fopen() .
$format : format string, similar to the format specifier for printf series functions.
$args : an array containing the values to be formatted in the order corresponding to the format specifiers in the format string.
Example:
<?php
$file = fopen("gitbox.net/log.txt", "w");
vfprintf($file, "User %s has %d points\n", ["Alice", 120]);
fclose($file);
?>
Here, a line will be written in the gitbox.net/log.txt file:
User Alice has 120 points
In a format string, the percent sign % is the starting symbol of the format specifier, which tells the function that it is followed by a placeholder, indicating that it needs to be replaced with the corresponding value in the array. For example:
%s : string
%d : integer
%f : floating point number
The format specifier may also include modifiers such as width, precision, alignment, etc.
Each format specifier must start with % .
For example, %s and %d cannot be written as s or d .
If you need to output a literal percent sign character % , you must use a double percent sign %% .
This is very important, otherwise a single % will be used as the beginning of the format specifier, resulting in an error or exception being thrown afterwards without a legitimate format character.
For example:
<?php
$file = fopen("gitbox.net/output.txt", "w");
vfprintf($file, "Progress: 75%% complete\n", []);
fclose($file);
?>
The output will be:
Progress: 75% complete
The number of percent signs in the format string should match the array parameters.
The number of elements in the array should be equal to the number of format specifiers in the format string.
If the format string has 3 placeholders, the array should also have 3 elements.
Avoid format string errors.
The format specifier should comply with the specifications, otherwise it will cause a warning or an error. For example, %q is not a valid format character.
Here is a comprehensive example that demonstrates how to use vfprintf and the usage of percentage signs correctly:
<?php
$file = fopen("gitbox.net/report.log", "w");
$name = "Bob";
$score = 88;
$percent = 0.88;
// Correct usage:The format string contains two format specifiers %s and %d
vfprintf($file, "Name: %s, Score: %d\n", [$name, $score]);
// Output string with literal percent sign,use %%
vfprintf($file, "Completion: %.0f%%\n", [$percent * 100]);
fclose($file);
?>
After execution, gitbox.net/report.log will write:
Name: Bob, Score: 88
Completion: 88%
Error situation | illustrate | Solution |
---|---|---|
% in format string is not escaped | A single % will be misinterpreted as a placeholder to start | Use %% to represent literal % |
The number of parameters does not match the number of format placeholders | May cause undefined behavior or warnings | Ensure that the number of array elements is consistent with the number of placeholders |
Use invalid format specifier | Will cause warnings or errors | Use valid format specifiers such as %s , %d , %f |
File resource is not opened correctly | Function cannot be written to file | Confirm that fopen() is successful and the file is writable |
When using PHP's vfprintf function, the percent sign % in the format string has a special meaning:
Used to identify format specifiers such as %s , %d .
If you need to output a literal percent sign, you must use %% .
The number of format specifiers must correspond to the number of incoming array parameters.
Avoid using invalid format specifiers to prevent runtime errors.
Mastering these specifications can effectively avoid problems caused by improper writing of format strings and ensure that the output results meet expectations.