How to use fprintf in PHP
In this article I will explain how the fprintf() function can be used in PHP.
fprintf() function in PHP
-
The fprintf() function is used to writea a formatted string to a specified output stream.
-
The fprintf() function returns the length of the outputted string.
-
In fprintf() function, the arg1, arg2, arg++ parameters will be added at percent (%) signs in the main string.
-
The fprintf() function works "step-by-step".
Syntax
fprintf(stream,format,arg1,arg2,arg++) |
Parameter
- stream stream is required parameter. Determine to write or output the string.
-
format format is required parameter. how to format the variables in it. Possible format values:
- %% - It returns a percent sign.
- %b - It is represent Binary number.
- %c - It is represent the character according to the ASCII value.
- %d - It is represent signed decimal number.
- %e - It is represent scientific notation.
- %u - It is represent unsigned decimal number.
- %f - It is represent floating-point number.
- %F - It is represent floating-point number.
- %o - It is represent octal number.
- %s - It is represent string.
- %x - It is represent hexadecimal number (lowercase letters).
- %X - It is represent hexadecimal number (uppercase letters).
- arg1 arg1 is required parameter. The argument to be added at the first %-sign in the format string.
- arg2 arg2 is optional parameter. The argument to be added at the second %-sign in the format string.
- arg++ It is optional parameter. The argument to be added at the second %-sign in the format string.
Example
The following example show to how the fprintf() function can be used in PHP.
<html>
<body>
<h3 style="color: darkblue;">fprintf() function example in PHP</h3>
<?php
$string1 = 'My emp_id is :';
$emp_id = 14;
$file_handler = fopen('fprintdemo.txt','w');
fprintf($file_handler, '%s %u', $string1, $emp_id);
fclose($file_handler);
// Let open the file once again and read the content.
$file_name = 'fprintdemo.txt';
$file_handler = fopen($file_name, 'r');
$file_data = fread($file_handler, 1024);
fclose($file_handler);
echo ($file_data);
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here