Função fprintf() do PHP
Exemplo
Escrever texto no arquivo de texto chamado "test.txt":
<?php $number = 9; $str = "Pequim"; $file = fopen("test.txt", "w"); echo fprintf($file,"Existem 쥕hões de bicicletas em %s.",$number,$str); ?>
Saída do código acima:
40
O seguinte texto será escrito no arquivo "test.txt":
Existem 9 milhões de bicicletas em Pequim.
Definição e uso
A função fprintf() escreve a string formatada no fluxo de saída especificado (por exemplo: arquivo ou banco de dados).
arg1,arg2,arg++ Os parâmetros serão inseridos no sinal de porcentagem (%) da string principal. A função é executada passo a passo. No primeiro símbolo %, insira arg1, insira arg2, em seguida, assim por diante.
Comment:Se o símbolo % for maior arg Se houver mais parâmetros, você deve usar placeholders. Os placeholders são inseridos após o símbolo %, composto por números e "\$". Veja o exemplo 2.
Funções relacionadas:
Sintaxe
fprintf(stream,format,arg1,arg2,arg++)
Parâmetros | Descrição |
---|---|
stream | Obrigatório. Define onde escrever/sair a string: |
format |
Obrigatório. Define a string e como formatar as variáveis dentro dela: Valores de formatação possíveis:
O valor de formatação adicional. É necessário inserir entre % e a letra (por exemplo, %.2f):
Comment:If multiple additional format values are used, they must be used in the order specified above. |
arg1 | Required. Specifies the insertion point format The first % symbol in the string. |
arg2 | Optional. Specifies the insertion point format The second % symbol in the string. |
arg++ | Optional. Specifies the insertion point format The third, fourth, etc. % symbols in the string. |
Technical details
Return value: | Returns the length of the written string. |
PHP version: | 5+ |
More examples
Example 1
Write text to file:
<?php $number = 123; $file = fopen("test.txt", "w"); fprintf($file, "%%f", $number); ?>
The following text will be written to the file "test.txt":
123.000000
Example 2
Use placeholders:
<?php $number = 123; $file = fopen("test.txt", "w"); fprintf($file, "Two decimal places: %%f", $number); \nNo decimal: %1$u ?>
The following text will be written to the file "test.txt":
Two decimal places: 123.00 No decimal: 123
Example 3
Use printf() to demonstrate 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); // Número decimal sem sinal (positivo) printf("%鑾u <br>", $num2); // Número decimal sem sinal (negativo) printf("%%f = %f <br>", $num1); // Número de ponto flutuante (considera a configuração local) printf("%%F = %F <br>", $num1); // Número de ponto flutuante (não considera a configuração local) printf("%%g = %g <br>", $num1); // Curto para %e e %f printf("%%G = %G <br>", $num1); // Curto para %E e %f printf("%%o = %o <br>", $num1); // Número octal printf("%%s = %s <br>", $num1); // String printf("%%x = %x <br>", $num1); // Número hexadecimal (minúsculo) printf("%%X = %X <br>", $num1); // Número hexadecimal (maiúsculo) printf("%%+d = %+d <br>", $num1); // Sinal de indicação (positivo) printf("%%+d = %+d <br>", $num2); // Explicação do sinal (negativo) ?>