PHP Knowledge Base
printf
and placeholders
Simple Example
Code
printf('%s and %s went up the hill.', 'Jack', 'Jill');
Output
Jack and Jill went up the hill.
Parameter Option Example
Code
printf('%1$s, %5$s, %4$s, %2$s, %6$s, %3$s, %7$s', 'Do', 'Fa', 'La', 'Mi', 'Re', 'Sol', 'Ti');
Output
Do, Re, Mi, Fa, Sol, La, Ti
Leading Zero Example [1]
Code
printf('May %02d', 2);
Output
May 02
Leading Zero Example [2]
Code
$format = "%02d";
printf("May $format", 2);
Output
May 02
Leading Zero Example [3]
Code
<?php
$format = "%02d";
$twoDigitDayNumber = sprintf($format, 2);
echo 'May '.$twoDigitDayNumber;
?>
Output
May 02
NULL