Función fprintf() de PHP
Ejemplo
Escribe el texto en el archivo de texto llamado "test.txt":
<?php $number = 9; $str = "Beijing"; $file = fopen("test.txt", "w"); echo fprintf($file,"Hay 쥕lones de bicicletas en %s.",$number,$str); ?>
La salida del código anterior es:
40
El siguiente texto se escribirá en el archivo "test.txt":
Hay 9 millones de bicicletas en Pekín.
Definición y uso
La función fprintf() escribe la cadena formateada en el flujo de salida especificado (por ejemplo: archivo o base de datos).
arg1、arg2、arg++ Los parámetros se insertarán en el símbolo (%) del string principal. La función se ejecuta paso a paso. En el primer símbolo %, inserte arg1, inserte arg2, por lo tanto, etcétera.
Comment:Si hay más símbolos % arg Si hay más parámetros, debe usar placeholders. Los placeholders se insertan después del símbolo %, y se componen de números y "\$". Véase el ejemplo 2.
Funciones relacionadas:
Sintaxis
fprintf(stream,format,arg1,arg2,arg++)
Parámetros | Descripción |
---|---|
stream | Obligatorio. Define dónde escribir/osalida la cadena. |
format |
Obligatorio. Define la cadena y cómo formatear los variables dentro de ella. Valores de formato posibles:
El valor de formato adicional. Es necesario colocarlo entre % y una letra (por ejemplo %.2f):
Comment:If multiple additional format values are used, they must be used in the order specified above. |
arg1 | Required. Specifies what to insert format The parameter at the first %% symbol in the string. |
arg2 | Optional. Specifies what to insert format The parameter at the second %% symbol in the string. |
arg++ | Optional. Specifies what to insert format The parameters at 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 sin signo (positivo) printf("%鑾u <br>", $num2); // Número decimal sin signo (negativo) printf("%%f = %f <br>", $num1); // Número de coma flotante (considerando la configuración local) printf("%%F = %F <br>", $num1); // Número de coma flotante (sin considerar la configuración local) printf("%%g = %g <br>", $num1); // Más corto que %e y %f printf("%%G = %G <br>", $num1); // Más corto que %E y %f printf("%%o = %o <br>", $num1); // Número octal printf("%%s = %s <br>", $num1); // Cadenas printf("%%x = %x <br>", $num1); // Número hexadecimal (en minúsculas) printf("%%X = %X <br>", $num1); // Número hexadecimal (en mayúsculas) printf("%%+d = %+d <br>", $num1); // Símbolo de indicación (positivo) printf("%%+d = %+d <br>", $num2); // Explicación de los símbolos (negativo) ?>