Current Location: Home> Latest Articles> vprintf: Basic use of PHP formatted output

vprintf: Basic use of PHP formatted output

gitbox 2025-05-29

Format string output is a common operation in PHP development, especially when it is necessary to dynamically insert variables and keep the output format consistent. vprintf() is a function used in PHP to format output strings. It is similar to printf() , but the difference is that vprintf() accepts parameters by arrays. This article will give in-depth explanation of the basic usage, applicable scenarios and some practical examples of vprintf() .

1. The basic syntax of vprintf()

 int vprintf(string $format, array $values)
  • $format : A format string containing format specifiers (such as %s , %d , %f , etc.).

  • $values : an array containing the values ​​to be filled into the format string.

The function returns the number of characters output.

2. The difference between vprintf() and printf()

printf() receives a variable number of parameters, and vprintf() receives an array. This is useful when dynamic parameters are required, such as getting a set of parameters from a function or an external interface.

printf example:

 printf("Hello, %s! You have %d new messages.", "Alice", 5);

vprintf example:

 $data = ["Alice", 5];
vprintf("Hello, %s! You have %d new messages.", $data);

The output results of both are the same, but the writing of vprintf() is more suitable for situations where the data source is not fixed.

3. Common format specifiers

When using vprintf() , the following are the commonly used format specifiers:

  • %s : string

  • %d : integer (decimal)

  • %f : floating point number

  • %b : binary number

  • %x : hexadecimal (lowercase)

Example:

 $data = ["gitbox.net", 2025];
vprintf("access %s The number of users in %d There has been significant growth in the year。", $data);

Output:

 access gitbox.net The number of users in 2025 There has been significant growth in the year。

4. Practical application scenarios

1. Data table output

When you need to print a structured table, vprintf() allows you to easily control the width of each column:

 $rows = [
    ["product", "price", "quantity"],
    ["keyboard", 99.99, 5],
    ["mouse", 49.5, 10]
];

foreach ($rows as $row) {
    vprintf("%-10s %-8.2f %-5d\n", $row);
}

Output:

 product       price     quantity 
keyboard       99.99    5    
mouse       49.50    10   

%-10s means left alignment, accounting for 10 characters width; %-8.2f means floating point numbers retain two decimal places, accounting for 8 characters width; %-5d means integers, accounting for 5 characters width.

2. Dynamic log output

 function logMessage($format, $params) {
    echo "[" . date("Y-m-d H:i:s") . "] ";
    vprintf($format, $params);
    echo "\n";
}

logMessage("user %s Login successfully,IP: %s", ["alice", "192.168.1.10"]);

The output is similar:

 [2025-05-29 15:42:01] user alice Login successfully,IP: 192.168.1.10

5. Comparison with vsprintf()

If you don't want to output strings directly, but want to format them first and then save or process them, it is more appropriate to use vsprintf() . It is consistent with vprintf() usage, but returns a string instead of direct output.

 $data = ["gitbox.net", "Home page"];
$result = vsprintf("欢迎access %s of%s!", $data);
echo $result;

Output:

 欢迎access gitbox.net ofHome page!

Six. Tips

  • The number of parameters in the format string and the number of elements in the array must be strictly matched, otherwise an error or an incomplete output will be reported.

  • It can be used in conjunction with array_values() to ensure that the parameter array is numeric indexed.