English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP printf() Function Usage and Examples

    PHP String Manual of String Functions

    The printf() function is used to output formatted strings.

Syntax

int printf ( string $format[, mixed $args[, mixed $... ]])

Definition and usage

Returns the formatted output string

Return value

It returns the length of the output string.

Parameter

Serial numberParameters and descriptions
1

format

Specify the string and how to format the variables within it.

Possible format values:

  • %% - return a percent sign %

  • %b - binary number

  • %c - character corresponding to ASCII value

  • %d - decimal number with a sign (negative, 0, positive)

  • %e - Use lowercase scientific notation (for example 1.2e+2

  • %E - Use uppercase scientific notation (for example 1.2E+2

  • 㩵n - decimal number without a sign (greater than or equal to 0)

  • %f - floating-point number (local setting)

  • %F - floating-point number (non-local setting)

  • %g - short %e and %f

  • %G - short %E and %f

  • %o - octal number

  • %s - string

  • %x - hexadecimal number (lowercase letters)

  • %X - hexadecimal number (uppercase letters)

additional format values. Must be placed between % and the letter (for example %.2f):

  • + (add a prefix to the number before + or - to define the sign of the number. By default, only negative numbers are marked, and positive numbers are not marked)

  • Specify what to use as padding, the default is space. It must be used with the width specifier. For example: '%x20s (Use "x" as padding)

  • - (Left align the variable value)

  • ][0-9] (Specify the minimum width of the variable value)

  • ][0-9] (Specify the number of decimal places or maximum string length)

Note:If multiple of the above format values are used, they must be used in the order specified above and cannot be disrupted.

2

arg1

Required. Specify the parameter to be inserted into the first % symbol in the format string.

3

arg2

Optional. Specify the parameter to be inserted into the second % symbol in the format string.

4

arg ...

Optional. Specify the parameter to be inserted into the format string at the third, fourth, and so on % symbols.

Online example

Try the following example, output formatted data and string:

<?php
    ////Output formatted string
    printf("www.oldtoolbag.com simply easy learning\n");
    //Use the format value %f to format the number:
    $number = 2123;
    printf("%f", $number);
    $str = "0758 jian";
    $strA = "A";
    $strB = "B";
    $num1 = 5;
    $num2 = 5;
    $num3 = 0.25;
    $num4 = 3.2567;
    $num5 = 8;
    $num6 = 1.735;
    $num7 = 16777215;
    $num8 = 16777215;
    echo '<br />';
    printf("%s %s", $strA, $strB); 
    echo '<br />';
    printf("Padding: %'%"10s //Specify the padding character as %string width as10
    echo '<br />';
    printf("Binary: %b", $num)1);
    echo '<br />';
    printf("ASCII code: %c", $num)2);
    echo '<br />';
    printf("Integer: %d", $num)3);
    echo '<br />';
    2f4);
    echo '<br />';
    printf("Octal: %o", $num)5);
    echo '<br />';
    printf("String: %s", $str);
    echo '<br />';
    printf("Non-decimal: 眻, $num)6);
    echo '<br />';
    printf("Hexadecimal: %x", $num)7);
    echo '<br />';
    printf("Hexadecimal: %X", $num)8);   
?>
Test and see‹/›

Output result

www.oldtoolbag.com simply easy learning
2123.000000A2 B1
Padding: %0758 jian
Binary: 101
ASCII code: 
Integer: 0
Floating-point: 3.26
Octal: 10
String: 0758 jian
Non-decimal: 1
Hexadecimal: ffffff
Hexadecimal: FFFFFF

PHP String Manual of String Functions