PHP vfprintf() 函数
PHP vfprintf() función
Ejemplo
<?php Escribe el texto en el archivo de texto llamado "test.txt": $number = 9; $file = fopen("test.txt","w"); $str = "Pekín"; ?>
echo vfprintf($file,"Hay 쥕lones de bicicletas en %s.",array($number,$str));
El código anterior emitirá:
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 vfprintf() escribe la cadena formateada en el flujo de salida especificado (por ejemplo: archivo o base de datos).
Comment:Diferente de fprintf(), los parámetros en vfprintf() están en un array. Los elementos del array se insertan en el símbolo (%) del string principal. La función se ejecuta paso a paso. En el primer símbolo %, se inserta el primer elemento del array, en el segundo símbolo %, se inserta el segundo elemento del array, y así sucesivamente. arg Si hay más parámetros, debe usar placeholders. Los placeholders se insertan después del símbolo %, compuestos por un número y "\$". Véase el ejemplo 2.
Funciones relacionadas:
Sintaxis
vfprintf(stream,format,argarray)
Parámetros | Descripción |
---|---|
stream | Requerido. Define dónde escribir/emitir la cadena. |
format |
Requerido. Define la cadena y cómo formatear los variables dentro de ella. Valores de formato posibles:
Valor de formato adicional. Es necesario colocarlo entre % y una letra (por ejemplo %.2f):
Comment:If multiple format values are used, they must be in the order specified above. |
argarray | Required. An array with parameters, which will be inserted format The % symbol in the string. |
Technical details
Return value: | Returns the length of the written string. |
PHP version: | 5+ |
More examples
Example 1
Write text to the file:
<?php $num1 = 123; $num2 = 456; $file = fopen("test.txt","w"); vfprintf($file,"%f%f",array($num1,$num2)); ?>
The following text will be written to the file "test.txt":
123.000000456.000000
Example 2
Use placeholders:
<?php $number = 123; $file = fopen("test.txt","w"); vfprintf($file,"With 2 decimals: %1\$.2f \nWithout decimals: %1$u",array($number)); ?>
The following text will be written to the file "test.txt":
With 2 decimals: 123.00 Without decimals: 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 the 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); // Corto en comparación con %e y %f printf("%%G = %G <br>", $num1); // Corto en comparación con %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 (minúsculas) printf("%%X = %X <br>", $num1); // Número hexadecimal (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) ?>