PHP writes a string to the specified output stream function fprintf ()

Source: Internet
Author: User

Instance

Write some text to a text file named "Test.txt":

<?php$number = 9; $str = "Beijing"; $file = fopen ("Test.txt", "w"); Echo fprintf ($file, "there is%u million bicycles in%s . ", $number, $str);? >

The above code will output:

40

The following text will be written to the file "Test.txt":

There is 9 million bicycles in Beijing.

Definition and usage

The fprintf () function writes a formatted string to the specified output stream (for example, a file or database).

The Arg1, arg2, + + parameters are inserted into the percent sign (%) symbol in the main string. This function is executed step-by. At the first% symbol, insert arg1, insert arg2 at the second% symbol, and so on.

Note: If the% symbol is more than the arg parameter, you must use a placeholder. Placeholders are inserted after the% symbol, consisting of numbers and "\$". See Example 2.

Tip: Related functions: printf (), sprintf (), vprintf (), vsprintf (), and vfprintf ()

Grammar

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 variable.

Possible format values: percent percent-returns a percent semicolon%

%b-Binary number

The character that corresponds to the%C-ASCII value

%d-decimal number with sign (negative, 0, positive)

%e-Using the lowercase scientific notation (e.g. 1.2e+2)

%E-Use uppercase scientific notation (e.g. 1.2E+2)

%u-decimal number with no sign (greater than or equal to 0)

%f-Floating point number (local setting)

%F-Floating point number (non-local setting)

%g-Shorter%e and%f

%G-Shorter%E and%f

%o-Eight binary number

%s-String

%x-16 binary number (lowercase letters)

%x-16 decimal digits (uppercase letters)

The attached format value. Must be placed between% and letter (e.g.%.2f):

+ (precede the number with + or-to define the positive and negative of the number.) By default, only negative numbers are marked, positive numbers are not marked.

' (specifies what to use as a fill, the default is a space.) It must be used with the width of the specified device. For example:% ' x20s (using "X" as the fill))

-(left adjustment variable value)

[0-9] (minimum width of the specified variable value)

. [0-9] (Specify the number of decimal digits or the maximum string length)

Note: If you use more than one of the above format values, they must be used in the order above and cannot be disrupted.

Arg1 required. Specifies the parameter that is inserted into the first% symbol in the format string.

Arg2 is optional. Specifies the parameter that is inserted into the second% symbol in the format string.

arg++ is optional. Specifies the parameter that is inserted into the format string third to fourth, and so on, in the% symbol.

Technical details

Return value: Returns the length of the string being written.

PHP version: 5+

More examples

Write some files to a 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, "with 2 decimals:%1$.2fnwith no decimals:%1$u", $ number);? >

The following text will be written to the file "Test.txt":

With 2 Decimals:123.00with no decimals:123

Example 3

Use printf () to demonstrate all possible format values:

&LT;?PHP$NUM1 = 123456789; $num 2 = -123456789; $char = 50; The ASCII Character is 2//note:the format value "percent" returns a percent signprintf ("%%b =%b <br>", $num 1); Binary numberprintf ("%%c =%c <br>", $char); The ASCII characterprintf ("%%d =%d <br>", $num 1); Signed decimal numberprintf ("%%d =%d <br>", $num 2); Signed decimal numberprintf ("%%e =%e <br>", $num 1); Scientific notation (lowercase) printf ("%%e =%E <br>", $num 1); Scientific notation (uppercase) printf ("%%u =%u <br>", $num 1); Unsigned decimal number (positive) printf ("%%u =%u <br>", $num 2); Unsigned decimal number (negative) printf ("%%f =%f <br>", $num 1); Floating-point number (Local settings Aware) printf ("%%f =%F <br>", $num 1); Floating-point number (not local settings aware) printf ("%%g =%g <br>", $num 1); Shorter of%e and%fprintf ("%%g =%G <br>", $num 1); Shorter of%E and%fprintf ("%%o =%o <br>", $num 1); OctaL numberprintf ("%%s =%s <br>", $num 1); stringprintf ("%%x =%x <br>", $num 1); Hexadecimal number (lowercase) printf ("%%x =%x <br>", $num 1); Hexadecimal number (uppercase) printf ("%%+d =%+d <br>", $num 1); Sign specifier (positive) printf ("%%+d =%+d <br>", $num 2); Sign specifier (negative)?>

Use the format value%f:

<?php $number = 123; printf ("%f", $number);?>

A demo of the string specifier:

<?PHP$STR1 = "Hello"; $str 2 = "Hello world!"; printf ("[%s]<br>", $str 1);p rintf ("[%8s]<br>", $str 1);p rintf ("[%-8s]<br>", $str 1);p rintf ("[%08s] <br> ", $str 1);p rintf (" [% ' *8s]<br> ", $str 1);p rintf (" [%8.8s]<br> ", $str 2);? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.