Showing posts with label PHP fprintf. Show all posts
Showing posts with label PHP fprintf. Show all posts

Thursday, 25 September 2014

fprintf in PHP

The PHP fprintf() function is utilized to writes a formatted string to a output stream.

Syntax:

fprintf(stream,format,arg1,arg2,arg++)
 
Parameters Description: 
stream : Required. Defines where to write the string.
format : Required. Defines the string and how to format the variables in it.
Possible format values:
  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number
  • %e - Scientific notation (e.g. 1.2e+2)
  • %u - Unsigned decimal number
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)
arg1 : Required. The argument to be added as the first %-sign.
arg2 : Optional. The argument to be added at the second %-sign.
arg++ : Optional.  The argument to be added at the third, fourth, etc. %-sign.
Note: If there are more % signs than arguments, you must use placeholders.
Tip: Related functions: printf(), sprintf(), vfprintf(), vprintf(), and vsprintf().

Example:

<?php 
$input_str = "Good";
$input_number = 330;
$file_name = fopen("test.txt","w");
echo fprintf($file_name,"%s Morning. It is day number %u",$input_str,$input_number);
?>

O/P:

34
The following text will be written to the file "test.txt":
Good Morning. It is day number 330