PHP vfprintf() function
example
Write the text to the text file named "test.txt":
<?php $number = 9; $str = "Beijing"; $file = fopen("test.txt","w"); echo vfprintf($file,"There are 쥕lion bicycles in %s.",array($number,$str)); ?>
The above code will output:
40
The following text will be written to the file "test.txt":
There are 9 million bicycles in Beijing.
definition and usage
The vfprintf() function writes the formatted string to the specified output stream (for example: file or database).
Unlike fprintf(), the parameters in vfprintf() are located in an array. Array elements will be inserted into the main string at the percentage (%) symbol. This function is executed step by step. At the first % symbol, the first array element is inserted, at the second % symbol, the second array element is inserted, and so on.
মুক্তপত্র:if % symbol is more than arg parameter, then you must use a placeholder. The placeholder is inserted after the % symbol, composed of a number and "\$". See example 2.
related functions:
syntax
vfprintf(stream,format,argarray)
parameter | description |
---|---|
stream | অপরিহার্য |
format |
অপরিহার্য সম্ভাব্য ফরম্যাট মান
অতিরিক্ত ফরম্যাট মান
মুক্তপত্র:একাধিক ফরম্যাট মানা ব্যবহার করা হলে, তারা উপরোক্ত ক্রমে বার্তায় থাকবে। |
argarray | অপরিহার্য। একটি পারামিটার সহ একটি আইনসংকলন, যা পরে প্রবেশ করবে। format স্ট্রিং-এর %% সংকেতে। |
প্রযুক্তিগত বিবরণ
ফলাফল: | লেখা হওয়া চিহ্নিত স্ট্রিং-এর দৈর্ঘ্য ফিরিয়ে দেয়。 |
PHP সংস্করণ: | 5+ |
আরও উদাহরণ
উদাহরণ 1
টেক্সট ফাইলে লেখা হবে:
<?php $num1 = 123; $num2 = 456; $file = fopen("test.txt","w"); vfprintf($file,"%f%f",array($num1,$num2)); ?>
নিম্নলিখিত টেক্সট "test.txt" ফাইলে লেখা হবে:
123.000000456.000000
উদাহরণ 2
প্রতিস্থাপক ব্যবহার করুন:
<?php $number = 123; $file = fopen("test.txt","w"); vfprintf($file,"With 2 decimals: %1$.2f \nকোনো দশমিক নেই: %1$u",array($number)); ?>
নিম্নলিখিত টেক্সট "test.txt" ফাইলে লেখা হবে:
2 দশমিক নেই: 123.00 কোনো দশমিক নেই: 123
উদাহরণ 3
সমস্ত সম্ভাব্য ফরম্যাট মানা printf() দ্বারা প্রদর্শিত করুন:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII 字符 50 是 2 // 注释:格式值 "%%" 返回百分号 printf("%%b = %%b <br>", $num1); // 二进制数 printf("%%c = %%c <br>", $char); // ASCII 字符 printf("%%d = %%d <br>", $num1); // 带符号的十进制数 printf("%%d = %%d <br>", $num2); // 带符号的十进制数 printf("%%e = %%e <br>", $num1); // 科学计数法(小写) printf("%%E = %%E <br>", $num1); // 科学计数法(大写) printf("%鑾u <br>", $num1); // Unsigned Decimal Number (Positive) printf("%鑾u <br>", $num2); // Unsigned Decimal Number (Negative) printf("%%f = %f <br>", $num1); // Floating Point Number (Localized) printf("%%F = %F <br>", $num1); // Floating Point Number (Not Localized) printf("%%g = %g <br>", $num1); // Shorter than %e and %f printf("%%G = %G <br>", $num1); // Shorter than %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); // Positive Sign Indicator printf("%%+d = %+d <br>", $num2); // Negative Sign Indicator ?>