-1

I have this line:

$lang = 'Estimado %s, tiene un saldo disponible de <strong>%s</strong> USD para realizar llamadas';

Then I intended to use sprintf function to output a right sentence, this is the code:

$userName = "SomeName";
$account = "13.1288";
sprintf($lang, $userName, number_format($account, 2));

But the output is just:

SomeName

What is wrong?

NikiC
  • 98,796
  • 35
  • 186
  • 223
ReynierPM
  • 16,470
  • 44
  • 175
  • 337

2 Answers2

3

sprintf doesn't actually print the text, it returns it as a string.

You probably meant to use printf instead, or you can echo the results of your sprintf statement.

Mr. Llama
  • 19,374
  • 2
  • 54
  • 107
1

sprintf returns a string with the formatted text (hence the 's' at the start). You can echo it echo sprintf(...) or just use printf.

Nicolas Defranoux
  • 2,626
  • 1
  • 9
  • 12
  • `echo sprintf()` is an "antipattern". There is absolutely no reason that anyone should ever write `echo sprintf()` -- it should be `printf()` without `echo` every time. – mickmackusa Apr 13 '22 at 08:09