पीएचपी fprintf () समारोह
उदाहरण
"Test.txt" नाम की टेक्स्ट फ़ाइल में कुछ टेक्स्ट लिखें:
<?php
$number = 9;
$str = "Beijing";
$file = fopen("test.txt","w");
echo fprintf($file,"There are %u million bicycles in %s.",$number,$str);
?>
उपरोक्त कोड का आउटपुट होगा:
40
निम्नलिखित पाठ "test.txt" फ़ाइल में लिखा जाएगा:
There are 9 million bicycles in Beijing.
परिभाषा और उपयोग
fprintf() फ़ंक्शन एक निर्दिष्ट आउटपुट स्ट्रीम (उदाहरण: फ़ाइल या डेटाबेस) के लिए एक स्वरूपित स्ट्रिंग लिखता है।
मुख्य स्ट्रिंग में arg1, arg2, ++ पैरामीटर प्रतिशत (%) संकेतों पर डाले जाएंगे। यह फ़ंक्शन "चरण-दर-चरण" काम करता है। पहले % चिन्ह पर, arg1 डाला जाता है, दूसरे % चिन्ह पर, arg2 डाला जाता है, आदि।
नोट: यदि तर्कों से अधिक % चिह्न हैं, तो आपको प्लेसहोल्डर का उपयोग करना चाहिए। % चिह्न के बाद एक प्लेसहोल्डर डाला जाता है, और इसमें तर्क- संख्या और "\$" होता है। उदाहरण दो देखें।
युक्ति: संबंधित कार्य: प्रिंटफ () , स्प्रिंटफ (), वीप्रिंटफ (), बनामप्रिंटफ ( ) और वीएफप्रिंटफ ( )
वाक्य - विन्यास
fprintf(stream,format,arg1,arg2,arg++)
पैरामीटर मान
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. |
arg1 | Required. The argument to be inserted at the first %-sign in the format string |
arg2 | Optional. The argument to be inserted at the second %-sign in the format string |
arg++ | Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | लिखित स्ट्रिंग की लंबाई लौटाता है |
---|---|
पीएचपी संस्करण: | 5+ |
और ज्यादा उदाहरण
उदाहरण
किसी फ़ाइल में कुछ पाठ लिखें:
<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"%f",$number);
?>
निम्नलिखित पाठ "test.txt" फ़ाइल में लिखा जाएगा:
123.000000
उदाहरण
प्लेसहोल्डर्स का उपयोग:
<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1\$.2f
\nWith no decimals: %1\$u",$number);
?>
निम्नलिखित पाठ "test.txt" फ़ाइल में लिखा जाएगा:
With 2 decimals: 123.00
With no decimals: 123
उदाहरण
सभी संभावित प्रारूप मूल्यों को प्रदर्शित करने के लिए प्रिंटफ () का उपयोग करना:
<?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)
?>
❮ पीएचपी स्ट्रिंग संदर्भ