printf()
displays a string, sprintf()
returns it. However, printf()
has a return value - it returns the string length. In this case, the resultant string is 2 products
, and the length of this string is 10
.
From the documentation for printf()
:
Return Values:
Returns the length of the outputted string.
If you want to store the output in a variable, use sprintf()
instead.
$totalItemsCount = sprintf (ngettext ("%d product", "%d products", 2), 2);