0

I'm using sprintf() in PHP but it displays nothing on the page.

Here's what I'm doing:

$lang['request_paragraph']="Please check your email for a message from %s (%s). This message contains instructions for initiating the issuance of a new password for your participant number. If you did not receive the email message from %s within 10-15 minutes, please make sure that your email provider does not have a spam filter that is blocking our email from reaching your Inbox. You will not be able to receive email from us if your email provider is using a mail-blocking device. Click on the button below to send the validation email again if it appears to have been blocked or never received. You will only be able to re-send the validation email 3 more times.";

$company="Company Name";

$admin_email="admin@company.com";

sprintf($lang['request_paragraph'],$company,$admin_email,$company);

Doing an echo on each individual string displays each string correctly, so what am I doing wrong?

I need to use sprintf() because I'm working with language files and it makes it much more simple than splitting the paragraph into pieces in the language definitions.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
sveti petar
  • 3,637
  • 13
  • 67
  • 144

4 Answers4

6

sprintf returns a variable (a string).

You need printf

Pete
  • 1,289
  • 10
  • 18
  • 2
    Oh. Well. Now I feel silly. I'd actually been using printf in the rest of the language files, don't know how or why I switched to sprintf half way through the job. Works now, thanks. – sveti petar Mar 27 '12 at 10:24
0

edit to echo sprintf($lang['request_paragraph'],$company,$admin_email,$company);

dotoree
  • 2,983
  • 1
  • 24
  • 29
  • echo sprintf() is a synonym for printf() – Pete Mar 27 '12 at 10:24
  • `echo sprintf()` is an "antipattern". There is absolutely no reason that anyone should ever write `echo sprintf()` in any code for any reason -- it should be `printf()` every time. – mickmackusa Apr 08 '22 at 10:20
0

You need to echo the sprintf too.

Vodun
  • 1,377
  • 1
  • 10
  • 12
  • `echo sprintf()` is an "antipattern". There is absolutely no reason that anyone should ever write `echo sprintf()` in any code for any reason -- it should be `printf()` every time. – mickmackusa Apr 08 '22 at 10:20
0

sprintf function just do the sequential replace of placeholder (type specifiers) characters in the first string argument and does not echo,print or output anything but return the formatted string.

so your line of code:

sprintf($lang['request_paragraph'],$company,$admin_email,$company);

will be replaced by:

echo sprintf($lang['request_paragraph'],$company,$admin_email,$company);

or

print(sprintf($lang['request_paragraph'],$company,$admin_email,$company));
Farrukh
  • 3
  • 2
  • `echo sprintf()` is an "antipattern". There is absolutely no reason that anyone should ever write `echo sprintf()` in any code for any reason -- it should be `printf()` every time. – mickmackusa Apr 08 '22 at 10:20