Why use sprintf function in PHP?

Viewed 84490

I am trying to learn more about the PHP function sprintf() but php.net did not help me much as I am still confused, why would you want to use it?

Take a look at my example below.

Why use this:

$output = sprintf("Here is the result: %s for this date %s", $result, $date);

When this does the same and is easier to write IMO:

$output = 'Here is the result: ' .$result. ' for this date ' .$date;

Am I missing something here?

24 Answers

sprintf() is quite similar to printf(). If you know printf() in details then sprintf() and even vsprintf() is not really difficult to understand.

one of the things that differs sprintf() from printf() is that you will need declare a variable to catch the output from the function as it does not directly print/echo anything. Let see the following code snippets:

printf("Hello %s", "world"); // "Hello world"

sprintf("Hello %s", "world"); // does not display anything

echo sprintf("Hello %s", "world"); // "Hello world"

$a = sprintf("Hello %s", "world"); // does not display anything

echo $a;// "Hello world"

Hope that helps.

One "outputs", the other "returns", that's one of the main differences.

printf() Outputs

sprintf() Returns

You have to be careful when using sprintf in loops:

$a = 'Anton';
$b = 'Bert';
$c = 'Corni';
$d = 'Dora';
$e = 'Emiel';
$f = 'Falk';
$loops = 10000000;

$time = microtime(true);

for ($i = 0; $i < $loops; $i++)
{
    $test = $a . $b . $c . $d . $e . $f;
}

$concatTime = microtime(true) - $time;

$time = microtime(true);

for ($i = 0; $i < $loops; $i++)
{
    $test = "$a $b $c $d $e $f";
}

$concat2Time = microtime(true) - $time;

$time = microtime(true);

for ($i = 0; $i < $loops; $i++)
{
    $test = sprintf('%s %s %s %s %s %s', $a, $b, $c, $d, $e, $f);
}

$sprintfTime = microtime(true) - $time;

echo 'Loops: ' . $loops . '<br>';
echo '\'$a . $b . $c . $d . $e . $f\'' . ' needs ' . $concatTime  . 's<br>';
echo '"$a $b $c $d $e $f"' . ' needs ' . $concat2Time  . 's<br>';
echo 'sprintf(\'%s %s %s %s %s %s\', $a, $b, $c, $d, $e, $f)' . ' needs ' . $sprintfTime  . 's<br>';

Which leads to the following times (on my local machine with PHP 7.2):

Loops: 10000000

'$a . $b . $c . $d . $e . $f' needs 1.4507689476013s

"$a $b $c $d $e $f" needs 1.9958319664001s

sprintf('%s %s %s %s %s %s', $a, $b, $c, $d, $e, $f) needs 9.1771278381348s

I use it for messages to users or other "pretty" types of functionality. For instance if I know I'm going to use the user's name.

$name = 'Some dynamic name';

And have multiple messages to use in that situation. (I.e., blocking or following another user)

$messageBlock = 'You have blocked %s from accessing you.';
$messageFollow = 'Following %s is a great idea!';

You can create a general function that does something to the user and add this string, and no matter what the structure of the sentence it should look pretty nice. I always dislike just appending strings together and constantly using dot notation and closing and reopening strings just to make a sentence look good. I was a fan at first like most but this seems pretty useful when multiple strings need to be manipulated and you do not want to hard code the placement of the variable in every time.

Think of it, what looks better?

return $messageOne === true ? $name.'. Please use the next example' : 'Hi '.$name.', how are you?'

Or

$message = $messageOne === true ? 'Option one %s' 
: ($messageTwo === true ? 'Option Two %s maybe?' : '%s you can choose from tons of grammatical instances and not have to edit variable placement and strings');

return sprintf($message, $name);

Sure its an extra step but what if your conditional checks do a bunch of other functional things, then the quotations and appending starts to get in the way of coding functionally.

Related