The function sprintf()
format a string based on the order of the parameters defined in the first argument, which makes everything very dynamic, allowing you to format strings very flexibly.
An example of great utility is when you need to write data in different languages.
Formatting for templates, data output, etc.
Common cases are names of persons, personal treatment pronouns, addresses, etc.
The example below clarifies better:
// Define idioma. Modifique para "pt" e veja como é a saída dos dados.
$l = 'ja';
// tratamento pessoal
$t['ja'] = '様';
// nome da pessoa
$n['ja'] = array('name' => '太郎', 'surname' => '山田');
// endereço
$a['ja'] = array(
'postal_code' => '104-0045',
'country_name' => '日本',
'province_name' => '東京都',
'city_name' => '東京',
'city_subregion' => '中央区',
'county_name' => '明石町',
'building' => 'マンション名123'
);
// tratamento pessoal
$t['pt'] = 'Sr(a)';
// nome da pessoa
$n['pt'] = array('name' => 'Fulano', 'surname' => 'Silva');
// endereço
$a['pt'] = array(
'postal_code' => '06453-040',
'country_name' => 'Brasil',
'province_name' => 'SP',
'city_name' => 'Barueri',
'city_subregion' => '',
'county_name' => 'Alphaville',
'building' => 'Calçadas das Papoulas 123'
);
// regra de formatação para idioma japonês
$nf['ja'] = '%3$s %2$s %1$s'; // nome
$af['ja'] = '〒%1$s %2$s %3$s %4$s %5$s %6$s %7$s'; // endereço
// regra de formatação para idioma português
$nf['pt'] = '%1$s %2$s %3$s'; // nome
$af['pt'] = '%7$s %6$s'.(empty($a[$l]['city_subregion'])? '': '- %5$s').', %4$s - %3$s, %1$s, %2$s'; // endereço
// escreve o nome
echo sprintf($nf[$l], $t[$l], $n[$l]['name'], $n[$l]['surname']).'<br>';
// escreve o endereço
echo sprintf(
$af[$l],
$a[$l]['postal_code'],
$a[$l]['country_name'],
$a[$l]['province_name'],
$a[$l]['city_name'],
$a[$l]['city_subregion'],
$a[$l]['county_name'],
$a[$l]['building']
);
Note that at the time of writing, nothing changes. The structure of the data in the arrays also does not change. Both are equal. What makes the difference is the formatting rule, which informs the function sprintf()
how and where to concatenate the other parameters.
Try to get the same result without using the function. It becomes complicated with many conditionals in a tangle of codes. A nightmare.
This is an example only for 2 languages and is a very simple and "easy" case. There are more complex things, however, the above example is enough to ask the question.
Number formatting
There are several other uses of the function, such as converting a number in scientific notation format to decimal/float.
$n = '5.3882598876953E-5';
// Esse trecho é para obter o exponente. Não é relevante para a questão
if ($p = strpos($n, '-')) {
$decimals = strlen(substr($n, 0, $p - 1));
$exponent = substr($n, $p + 1);
$decimals += !empty($exponent)?$exponent - 1: 0;
} else {
$decimals = strlen(substr($n, strpos($n, '.') + 1));
}
echo $n.'<br>'.sprintf('%.'.$decimals.'f', 1 * $n);
Imagine doing it without the function sprintf()
.
Opinion
Finally, personal opinion, but without wishing to belittle anyone, I find it unnecessary to use it at all just to make the code beautiful. It makes no sense to use in cases where the format is static, for example. It would just be consuming unnecessary processes.
Example of situation where it is done unnecessary
$ch = curl_init(sprintf("%s/pages/%s/metrics/%s/data.json", $BASE_URI, $PAGE_ID, $METRIC_ID));
It makes no difference if you make an organized, standardized concatenation.
$ch = curl_init($BASE_URI.'/pages/'.$PAGE_ID.'/metrics/'.$METRIC_ID'./data.json');
As mentioned above, the format in this case is static. There is no need to use a dynamic formatting feature for something static.
$var1 = 'A';
$var2 = 'B';
$var3 = 'C';
echo $var1.''.$var2.''.$var3;
//Tempo: 596μs e algumas vezes a 610μs
// Não faz menor diferença usar vírgula ou ponto para concatenar.
$var1 = 'A';
$var2 = 'B';
$var3 = 'C';
echo sprintf('%1$s %2$s %3$s', $var1, $var2, $var3);
//Tempo: 786μs, porém, fica "flutuando" entre 880μs e 900μs
μs: microseconds
The difference for a single, simple line is 2 to 3 microseconds. Of course for a single execution it is irrelevant, however, if the application adopts the function to make all concatenations, add them. If you have 100 concatenations in the codes, it is no longer irrelevant. A loss of performance for an unnecessary "whim".
The examples have didactic purpose. They are merely illustrative.
I see no advantage in situations like this, I think it’s nice to use when the argument of
sprintf
is an argument of a function. You have to balance to see what is necessary, and what is foreign– Wallace Maxters
Hey, do you think you can score one of the answers?
– Wallace Maxters