fprintf() funtion in PHP || PHP fprintf function

fprintf() function in php

Last Updated by Pooja Chikara 02-May-20

0 328


PHP fprintf() function is used to writes a user formatted PHP string to a particular output stream like file or database. It is an inbuilt function of PHP.

Syntax:

fprintf($stream,$format,$arg1,$arg2,$arg++);

here,

1) $stream represents a file in which the resultant string will be written by this function. It is mandatory.

2) $format represents the format of the resultant string. It has 15 possible values. It is mandatory.

  • %%: It returns a percent sign.
  • %b: It used for binary numbers.
  • %c: It used to format characters according to ASCII values.
  • %e: It used for the scientific notation in lowercase.
  • %E: Scientific notation in uppercase.
  • %u: Negative unsigned decimal numbers.
  • %f: Floating point numbers ( local setting aware).
  • %F: Floating point numbers (Not local setting aware).
  • %g: Shorter of %e and %f.
  • %G: Shorter of %E and %f.
  • %o: Octal numbers.
  • %s: String
  • %x: Lowercased hexadecimal numbers.
  • %X: Uppercased hexadecimal numbers.

3) $arg1 represents an argument which inserted at the first formatted string. It is mandatory.

4) $arg2 represents an argument which inserted at the second formatted string. It is optional.

5) $arg++ is also optional and its inserted at the next formatted string.


Example:

<?php
$string = "Welcome to codingtag";
$stream = fopen("anyname.txt","w"); // open file to write the resultant string
fprintf($stream,"%s",$string);
$getstr=fopen("anyname.txt","r");
echo fread($getstr,filesize("anyname.txt"));
?>

Output:

fprintf() function in php


Share:


Leave a Reply


Comments
    Waiting for your comments