printf()


int printf ( string $format, bool/int/string $arg... )

Description

printf() outputs the formatted data to its standard output.
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

Parameters

format: %% - outputs a literal percent character
%b - outputs a binary number (argument: integer)
%c - outputs a character of ASCII value (argument: integer)
%d - outputs a decimal number (argument: integer)
%u - outputs an unsigned decimal number (argument: integer)
%o - outputs an octal number (argument: integer)
%e - outputs a scientific notation (argument: float)
%E - like %s but uses uppercase letter (argument: float)
%f - outputs 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 - outputs a string (argument:string)
%x - outputs a hexadecimal number with lowercase letters (argument: string)
%X - outputs 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 output string's length, PHP error on error

Example

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

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

$msg = 'sollae';

printf("[%s]\r\n",      $msg);  // standard string output
printf("[%10s]\r\n",    $msg);  // right-justification with spaces
printf("[%-10s]\r\n",   $msg);  // left-justification with spaces
printf("[%010s]\r\n",   $msg);  // zero-padding works on strings too
$ret = printf("[%'@10s]\r\n",  $msg);  // use the custom padding character ‘@’
echo "=>$ret bytes\r\n";
?>

See also

sprintf()

Remarks

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