string sprintf ( string $format, bool/int/string $args... )


sprintf() returns the formatted string. The parameters ($args …) will be inserted at percent sign(%) in the $format string. If parameter’s data type is different from the percent format, a juggling error occurs.

※ available F/W version : all


format: %% - writes a literal percent character
%b - writes a binary number (argument: integer)
%c - writes a character of ASCII value (argument: integer)
%d - writes a decimal number (argument: integer)
%u - writes an unsigned decimal number (argument: integer)
%o - writes an octal number (argument: integer)
%e - writes a scientific notation (argument: float)
%E - like %s but uses uppercase letter (argument: float)
%f - writes a floating point number (argument: float)
%F - same to %s
%g - shorter of %e and %f (argument: float)
%G - shorter of %E and %f (argument: float)
%s - writes a string (argument:string)
%x - writes a hexadecimal number with lowercase letters (argument: string)
%X - writes a hexadecimal number with uppercase letters (argument: string)
If a number is inserted between the percent sign(%) and the format letter, the number means that the digit of the data. If the number starts with a zero (0), blank will be replaced with 0.
If ‘+’ is inserted between the percent sign(%) and the format letter or number, it outputs sign (+/-) even though the number is positive.

Return values

Returns the formatted string, PHP error on error


$n =  43951789;
$u = -43951789;
$c = 65;        // decimal 65, hexadecimal 0x41, ‘A’

echo sprintf("%%b = %b\r\n", $n);         // binary representation
echo sprintf("%%c = %c\r\n", $c);         // print the ascii character, same as chr() function
echo sprintf("%%d = %d\r\n", $n);          // standard integer representation
echo sprintf("%%+d = %+d\r\n", $n);        // standard integer representation with sign
echo sprintf("%%u = %u\r\n", $n);          // unsigned integer representation of a positive integer
echo sprintf("%%u = %u\r\n", $u);          // unsigned integer representation of a negative integer
echo sprintf("%%o = %o\r\n", $n);          // octal representation
echo sprintf("%%e = %e\r\n", (float)$n);  // scientific notation
echo sprintf("%%E = %E\r\n", (float)$n);  // scientific notation with a upper case E
echo sprintf("%%f = %f\r\n", (float)$n);  // floating point notation
echo sprintf("%%F = %F\r\n", (float)$n);  // floating point notation
echo sprintf("%%g = %g\r\n", (float)$n);  // shorter form
echo sprintf("%%G = %G\r\n", (float)$n);  // shorter form
echo sprintf("%%s = %s\r\n", (string)$n); // string representation
echo sprintf("%%x = %x\r\n", $n);           // hexadecimal representation (lower-case)
echo sprintf("%%X = %X\r\n", $n);           // hexadecimal representation (upper-case)

$msg = 'sollae';

echo sprintf("[%s]\r\n",      $msg);  // standard string output
echo sprintf("[%10s]\r\n",    $msg);  // right-justification with spaces
echo sprintf("[%-10s]\r\n",   $msg);  // left-justification with spaces
echo sprintf("[%010s]\r\n",   $msg);  // zero-padding works on strings too

See also



This function is identical to the PHP group’s sprintf() function.