Home | Store | osCommerce Tutorials

PHP sprintf() Function

The PHP sprintf() function writes a formatted string to a variable..

The syntax of PHP sprintf() is:

sprintf(format, argument_1, argument_2)

The format is a required parameter. It specifies the string and how to format the variables in it.

The argument_1 is also a required parameter. This argument need to be inserted at the first %-sign in the format string.

The argument_2 is an optional parameter. This argument need to be inserted at the second %-sign in the format string.

More arguments can be used. However this is very rare to use more than two arguments.

Here's some of the common format:

  • %d - Signed decimal number
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %s - String

PHP sprintf() function example:

<?php

$myString = sprintf ("New Products For %s", "October");
echo $myString;

?>

The output of the above sprintf() function example should look like:

New Products For October

You should see the above sprintf() function example in includes/modules/new_products.php:

$info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));

Depending on the current month, the print out of the above code is something like "New Products For October".

TIPS:

  • The TABLE_HEADING_NEW_PRODUCTS is defined as "New Products For %s" in includes/languages/english/index.php. Here's the code:

    define('TABLE_HEADING_NEW_PRODUCTS', 'New Products For %s');

  • Click here to see the PHP strftime() function tutorial.

This is the end of PHP sprintf() function tutorial for osCommerce shop.