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="[email protected]";

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.

1
  • Where's the code to display something? Mar 27, 2012 at 10:23

4 Answers 4

6

sprintf returns a variable (a string).

You need printf

1
  • 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. Mar 27, 2012 at 10:24
0

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

2
  • echo sprintf() is a synonym for printf()
    – Pete
    Mar 27, 2012 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. Apr 8, 2022 at 10:20
0

You need to echo the sprintf too.

1
  • 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. Apr 8, 2022 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));
1
  • 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. Apr 8, 2022 at 10:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.