PHP printf() function

Example

Output the formatted string:

<?php
$number = 9;
$str = "北京";
printf("In %s there are 쥕lion bicycles.", $str, $number);
?>

Run Instances

Definition and usage

The printf() function outputs a formatted string.

arg1,arg2,arg++ Parameters will be inserted into the main string at the percentage (%) symbol. The function is executed step by step. At the first % symbol, insert arg1, insert arg2, and so on.

Comment:If there are more % symbols than arg If you have parameters, you must use placeholders. Placeholders are inserted after the % symbol, consisting of a number and "\$". See example 2.

Tip:Related functions: sprintf(), vprintf(), vsprintf(), fprintf(), and vfprintf()

Syntax

printf(format,arg1,arg2,arg++)
Parameter Description
format

Required. Specifies the string and how to format the variables within it.

Possible format values:

  • %% - Returns a percentage sign %
  • %b - Binary number
  • %c - Character corresponding to ASCII value
  • %d - Decimal number with a sign (negative, 0, positive)
  • %e - Scientific notation using lowercase letters (e.g., 1.2e+2)
  • %E - Scientific notation using uppercase letters (e.g., 1.2E+2)
  • 蒝ecimal number without a sign (greater than or equal to 0)
  • %f - Floating-point number (local settings)
  • %F - Floating-point number (non-local settings)
  • %g - Shorter than %e and %f
  • %G - Shorter than %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format values. They must be placed between % and a letter (e.g., %.2f):

  • The + or - symbol in front of a number to define its sign. By default, only negative numbers are marked, positive numbers are not.
  • The specified character to use as padding (default is a space). It must be used with the width specifier.
  • - (left-adjusts the variable value)
  • .[0-9] (specifies the minimum width of the variable value)
  • .[0-9] (specifies the number of decimal places or maximum string length)

Comment:If multiple of the above format specifiers are used, they must be used in the order specified above and cannot be mixed.

arg1 Required. Specifies the argument to be inserted format The parameter at the first % symbol in the string.
arg2 Required. Specifies the argument to be inserted format The parameter at the second % symbol in the string.
arg++ Optional. Specifies the argument to be inserted format The parameters at the third, fourth, and so on % symbols in the string.

Technical details

Return value: Returns the length of the output string.
PHP version: 4+

More examples

Example 1

Using format specifier %f:

<?php
$number = 123;
printf("%f",$number);
?>

Run Instances

Example 2

Using placeholders:

<?php
$number = 123;
printf("With two decimal places: %1$.2f<br>No decimals: %1$u",$number);
?>

Run Instances

Example 3

Demonstration of all possible format values:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // ASCII character 50 is 2
// Comment: The format value "%%" returns a percent sign
printf("%%b = %b <br>",$num1); // Binary number
printf("%%c = %c <br>",$char); // ASCII character
printf("%%d = %d <br>",$num1); // Signed decimal number
printf("%%d = %d <br>",$num2); // Signed decimal number
printf("%%e = %e <br>",$num1); // Scientific notation (lowercase)
printf("%%E = %E <br>",$num1); // Scientific notation (uppercase)
printf("%鑾u <br>",$num1); // Unsigned decimal number (positive)
printf("%鑾u <br>",$num2); // Unsigned decimal number (negative)
printf("%%f = %f <br>",$num1); // Floating-point number (respecting local settings)
printf("%%F = %F <br>",$num1); // Floating-point number (not respecting local settings)
printf("%%g = %g <br>",$num1); // Shorter than %e and %f
printf("%%G = %G <br>", $num1); // Shorter than %E and %f
printf("%%o = %o <br>", $num1); // Octal Number
printf("%%s = %s <br>", $num1); // String
printf("%%x = %x <br>", $num1); // Lowercase Hexadecimal Number
printf("%%X = %X <br>", $num1); // Uppercase Hexadecimal Number
printf("%%+d = %+d <br>", $num1); // Positive Sign Specifier
printf("%%+d = %+d <br>", $num2); // Negative Sign Specifier
?>

Run Instances

Example 4

Demonstration of String Specifiers:

<?php
$str1 = "Hello";
$str2 = "Hello world!";
printf("[%s]<br>", $str1);
printf("[%8s]<br>", $str1);
printf("[%-8s]<br>", $str1);
printf("[%08s]<br>", $str1);
printf("[%'*8s]<br>", $str1);
printf("[%8.8s]<br>", $str2);
?>

Run Instances