Using the sprintf() function

A sprintf() function that formats two values

<?php
  $message = sprintf('The book about %s has %d pages.','PHP', 800);
  // $message now: The book about PHP has 800 pages.
?>

How to use sprintf() to convert numbers to strings

<?php
  $s1 = sprintf('It cost %s dollars', 12);   // It cost 12 dollars
  $s2 = sprintf('%s', 4.5);                  // 4.5
  $s3 = sprintf('%s', 9451000.000000);       // 9451000
  $s4 = sprintf('%f', 9.451e6);              // 9451000.000000
  $s5 = sprintf('%e', 9451000.000000);       // 9.451000e+6
  $s6 = sprintf('%c', 65);                   // A
  $s7 = sprintf('%x', 15);                   // f
  $s8 = sprintf('%X', 15);                   // F
  $s9 = sprintf('%s%%', 4.5);                // 4.5%
?>

Back