vnsprintf()

Since version 1.0.0

Description

vnsprintf() - PHP's vsprintf() with added support for named arguments.

string vnsprintf(string $format, array $args);

Parameters

vnsprintf() takes two parameters:

PHP's built-in sprintf() already supports positional parameters:

echo vsprintf("The %1\$s sat on the %2\$s", ['cat', 'mat']) . PHP_EOL;
// output: The cat sat on the mat

vnsprintf() adds support for using named parameters too:

$args = [
    'animal' => 'cat',
    'place' => 'mat'
];
echo vnsprintf("The %animal\$s sat on the %place\$s", $args) . PHP_EOL;
// output: The cat sat on the mat

Return Values

vnsprintf() returns a string: the result of expanding $format using the data in $args.

Throws

vnsprintf() throws the following exception(s):

Constraints

Internally, vnsprintf() converts $format into a sprintf()-compatible format string. As a result, $format must be one of the following:

You cannot do the following:

If you do so, you'll get an error message from PHP.