PHP vfprintf() 함수
예시
"test.txt"라는 텍스트 파일에 일부 텍스트를 씁니다.
<?php
$number = 9;
$str = "Beijing";
$file = fopen("test.txt","w");
echo vfprintf($file,"There are %u million bicycles in %s.",array($number,$str));
?>
위 코드의 출력은 다음과 같습니다.
40
다음 텍스트는 "test.txt" 파일에 작성됩니다.
There are 9 million bicycles in Beijing.
정의 및 사용
vfprintf() 함수는 지정된 출력 스트림(예: 파일 또는 데이터베이스)에 형식화된 문자열을 씁니다.
fprintf()와 달리 vfprintf()의 인수는 배열에 배치됩니다. 배열 요소는 기본 문자열의 백분율(%) 기호에 삽입됩니다. 이 기능은 "단계적"으로 작동합니다. 첫 번째 % 기호에는 첫 번째 배열 요소가 삽입되고, 두 번째 % 기호에는 두 번째 배열 요소가 삽입되는 식입니다.
참고: 인수보다 % 기호가 더 많은 경우 자리 표시자를 사용해야 합니다. 자리 표시자는 % 기호 뒤에 삽입되며 인수 번호와 "\$"로 구성됩니다. 예 2를 참조하십시오.
팁: 관련 함수: fprintf() , printf() , sprintf() , vprintf() 및 vsprintf() .
통사론
vfprintf(stream,format,argarray)
매개변수 값
Parameter | Description |
---|---|
stream | Required. Specifies where to write/output the string |
format | Required. Specifies the string and how to format the variables in it. Possible format values:
Additional format values. These are placed between the % and the letter (example %.2f):
Note: If multiple additional format values are used, they must be in the same order as above. |
argarray | Required. An array with arguments to be inserted at the % signs in the format string |
기술적 세부 사항
반환 값: | 작성된 문자열의 길이를 반환합니다. |
---|---|
PHP 버전: | 5+ |
더 많은 예
예시
파일에 일부 텍스트 쓰기:
<?php
$num1 = 123;
$num2 = 456;
$file = fopen("test.txt","w");
vfprintf($file,"%f%f",array($num1,$num2));
?>
다음 텍스트는 "test.txt" 파일에 작성됩니다.
123.000000456.000000
예시
자리 표시자의 사용:
<?php
$number = 123;
$file = fopen("test.txt","w");
vfprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",array($number));
?>
다음 텍스트는 "test.txt" 파일에 작성됩니다.
With 2 decimals: 123.00
With no decimals: 123
예시
가능한 모든 형식 값을 보여주기 위해 printf() 사용:
<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2
// Note: The format value "%%" returns a percent sign
printf("%%b = %b <br>",$num1); // Binary number
printf("%%c = %c <br>",$char); // The 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 = %u <br>",$num1); // Unsigned decimal number (positive)
printf("%%u = %u <br>",$num2); // Unsigned decimal number (negative)
printf("%%f = %f <br>",$num1); // Floating-point number (local settings aware)
printf("%%F = %F <br>",$num1); // Floating-point number (not local settings aware)
printf("%%g = %g <br>",$num1); // Shorter of %e and %f
printf("%%G = %G <br>",$num1); // Shorter of %E and %f
printf("%%o = %o <br>",$num1); // Octal number
printf("%%s = %s <br>",$num1); // String
printf("%%x = %x <br>",$num1); // Hexadecimal number (lowercase)
printf("%%X = %X <br>",$num1); // Hexadecimal number (uppercase)
printf("%%+d = %+d <br>",$num1); // Sign specifier (positive)
printf("%%+d = %+d <br>",$num2); // Sign specifier (negative)
?>
❮ PHP 문자열 참조