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

Tutoriais Básicos do PHP

Tutoriais Avançados do PHP

PHP & MySQL

Manual de Referência do PHP

Uso e exemplo da função printf() do PHP

    PHP String 字符串函数手册

    A função printf() é usada para output de strings formatadas.

sintaxe

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

definição e uso

ele retorna a string formatada de saída

retorno

ele retorna o comprimento da string de saída.

parâmetro

númeroparâmetros e descrições
1

format

especificar uma string e como formatar as variáveis dentro dela.

valores de formatação possíveis:

  • %% - retornar um símbolo de porcentagem %

  • %b - número binário

  • %c - caractere correspondente ao valor ASCII

  • %d - número decimal com sinal (número negativo, 0, número positivo)

  • %e - usar a contagem científica em minúsculas (por exemplo 1.2e+2

  • %E - usar a contagem científica em maiúsculas (por exemplo 1.2E+2

  • 㩵n - número decimal sem sinal (maior ou igual a 0)

  • %f - número de ponto flutuante (configurado localmente)

  • %F - número de ponto flutuante (não configurado localmente)

  • %g - os mais curtos %e e %f

  • %G - os mais curtos %E e %f

  • %o - número octal

  • %s - cadeia de caracteres

  • %x - número hexadecimal (letras minúsculas)

  • %X - número hexadecimal (letras maiúsculas)

valores de formatação adicionais. É necessário coloca-los entre % e a letra (por exemplo %.2f):

  • + (adicionar antes do número + ou - definir a sinalidade de números. Por padrão, apenas números negativos são marcados, números positivos não são marcados)

  • Especifique o que usar como preenchimento, o padrão é espaço. Deve ser usado juntamente com o especificador de largura. Por exemplo: '%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 format values are used, they must be used in the order specified above and cannot be disordered.

2

arg1

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

3

arg2

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

4

arg ...

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

Online example

Try the following example, output formatted data and string:

<?php
    ////Output formatted string
    printf("pt.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", $str); //Specify the padding character as%string width as10
    echo '<br />';
    printf("Binary: %b", $num1);
    echo '<br />';
    printf("ASCII code: %c", $num2);
    echo '<br />';
    printf("Integer: %d", $num3);
    echo '<br />';
    printf("Floating-point: %.2f", $num4);
    echo '<br />';
    printf("Octal: %o", $num5);
    echo '<br />';
    printf("String: %s", $str);
    echo '<br />';
    printf("Non-decimal: 眻, $num6);
    echo '<br />';
    printf("Hexadecimal: %x", $num7);
    echo '<br />';
    printf("Hexadecimal: %X", $num8);   
?>
Test and see‹/›

Output result

pt.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 字符串函数手册