PHP String vprintf() Function
The vprintf() is an in-built string function of PHP. It is used to display the array values as a formatted string according to the format. The main purpose of this function is to display a formatted string. PHP 4 and above versions supports vprintf() string function.
The vprintf() function works similar to the printf() function, but it accepts an array of arguments. These array elements are inserted along with percent (%) signs in the main string. The vprintf() function works step-by-step. At each % sign, elements are inserted. It returns the length of the outputted string after successful execution.
Note: If % signs are more than the argument passed in the function, then we must use the placeholder. A placeholder is always inserted after the % sign, and contains the argument such as - number and "$".
Unlike vfprintf() function, it does not contains $stream argument that where to write the string.
The syntax of the vprintf() is given below, in which both arguments are mandatory to pass in this function.
$format (required) - It is a mandatory parameter of vprintf() function, which specifies that how a string is formatted.
Possible format values:
Additional format values:
These values are placed between the % sign and the letters, e.g., %.2f
Note: If multiple additional format values are used, then they must be in the same order as above.
$array_arg (required) - These are the array arguments which need to format. The arguments within an array are to be inserted at the % symbol in the format string.
$array_arg (required) The vprintf() function returns the length of the outputted string.
Below is the list of examples, by which we can learn the working of the vprintf() function.
In the below example, we will open a test.txt file in write mode ("w") to write some data in it. If test.txt file does not exist, it will automatically create the file in the same folder, and then we can write the text in it.
Unlike vprintf(), it displays the string data directly on the browser, rather than in a file.
There are 365 days in 1 year.
Example 2: Use of placeholder
In the above example, we have used the placeholder here, because % is more than the arguments passed in the function.
With two decimal point: 12.35 With three decimal point: 12.350 With no decimal: 12
The following data will be written in the test.txt file.