fprintfWRITES a formatted STRING to a stream.
This function is
SELF-SUFFICIENT, because it does not require auxiliary functions to display the result.
Undue substitutions will produce unpredictable results.
<?php
int fprintf ( res $handle , str $format [, mix $args [, mix $... ]] )
where,
$handle = The file system pointer as a resourse
( Typically created by fopen )
$format = The format STRING used to express the result
( SEE the below TABLE )
$args = Additional arguments
?>
$handle
The file system pointer as a resourse.
Typically created by fopen.
$format
As
sprintf, each
SPECIFIER, present in
FORMATTED STRING, must have its own
SUBSTITUTE ARGUMENT.
SPECIFIER % |
TYPE |
ARGUMENT |
COMMENT |
%% |
|
literally % |
|
%b |
INTEGER |
Represented as BINARY |
|
%c |
INTEGER |
Represented as CHARACTER - ASCII |
|
%d |
INTEGER |
Represented as DECIMAL NUMBER |
+/- |
%e |
FLOAT |
SCIENTIFIC NOTATION |
|
%E |
FLOAT |
As %e with the use of UPPERCASE
|
|
%u |
INTEGER |
Presented as an UNSIGNED DECIMAL number. |
no signal |
%f |
FLOAT |
Compatible with LOCALE |
|
%F |
FLOAT |
As %f not compatible with LOCALE |
|
%g |
FLOAT |
Shorter of %e and %f. |
|
%G |
FLOAT |
Shorter of %E e %F |
|
%o |
INTEGER |
Presented as an OCTAL number |
|
%s |
STRING |
STRING
|
|
%x |
INTEGER |
Presented as an HEXADECIMAL number |
lowercase |
%X |
INTEGER |
Presented as an HEXADECIMAL number |
UPERCASE |
ADDITIONAL FORMATS |
0 |
ZERO |
FILLING WITH ZERO |
Number of desired decimal places |
" " |
SPACE |
FILLING WITH SPACES |
Number of desired places |
- |
HYPHEN |
Left-aligned value |
Right-aligned as default |
N1.N2 |
N1 |
INTEGER
|
NUMBER OF DIGITS |
N2 |
DECIMAL |
NUMBER OF DIGITS |
ed48 |
$arg1 ... $argN
Additional arguments.
EXERCISE
<?php
$file01 = __DIR__ . '/browser.txt';
$browsr01 = $_SERVER['HTTP_USER_AGENT'];
// This is an internal variable
$fp = fopen($file01, 'w');
if($fp == TRUE)
{
fprintf($fp, '%s', $browsr01);
fclose($fp);
echo 'The file WAS SAVED as expected.<br><br>';
echo file_get_contents($file01) . '<br><br>';
unlink($file01);
/* - - - - - - - - - - - - - - - - - - - - - - - -
To erase the created $data_file.
However, if you want to keep it,
just comment this line.
- - - - - - - - - - - - - - - - - - - - - - - - */
}
else
{
echo 'The file WAS NOT SAVED as expected.<br><br>';
}
?>
RESULT
The file WAS SAVED as expected.
File content example, (browser.txt):
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
The content saved by you, in this file, can be different.
EXERCISE
<?php
$file02 = __DIR__ .'/fprintf02.txt';
$isodate02 = "%04d-%02d-%02d";
$year = idate('Y');
$month = idate('m');
$day = idate('d');
$h = idate('H');
$m = idate('i');
$s = idate('s');
$fp = fopen($file02, 'w');
if($fp == TRUE)
{
fprintf($fp, "%04d-%02d-%02d %02dh%02dmin%02ds",
$year, $month, $day, $h, $m, $s );
fclose($fp);
echo 'The file WAS SAVED as expected.<br><br>';
echo file_get_contents($file02) . '<br><br>';
unlink($file02);
/* - - - - - - - - - - - - - - - - - - - - - - - -
To erase the created $data_file.
However, if you want to keep it,
just comment this line.
- - - - - - - - - - - - - - - - - - - - - - - - */
}
else
{
echo 'The file WAS NOT SAVED as expected.<br><br>';
}
?>
EXERCISE
<?php
$float_variation = [ "%f","%-f", "%+f",
"%7.2f", "%-7.2f",
"%07.2f", "%-07.2f",
"%'#7.2f" ];
$float_numbers = [ 0, 1, -1, 0.32,
-0.32, 3.4. -3.4,
2.54, -2.54 ];
setlocale(LC_ALL, 'de-DE', 'de_DE');
/* creating dumping file */
$data_file = __DIR__ . '/fprintf03.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
$counter = 1;
/* float type variations */
fprintf($fp, "<br><br>Testing fprintf() with floats:<br>");
foreach( $float_variation as $float_var ) {
fprintf( $fp, "\n\nIteration: %d\n",$counter);
foreach( $float_numbers as $float_num ) {
fprintf( $fp, "\n");
fprintf( $fp, $float_var, $float_num );
}
$counter++;
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
/* To erase the created $data_file.
However, if you want to keep it,
just comment this line.
*/
?>
EXERCISE
<?php
$int_variation = array( "%d", "%-d",
"%+d", "%7.2d",
"%-7.2d", "%07.2d",
"%-07.2d", "%'#7.2d" );
$int_numbers = array( 0, 1, -1, 2.7,
-2.7, 23333333,
-23333333, "1234" );
/* creating dumping file */
$data_file = __DIR__ . '/fprintf04.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
$counter = 1;
/* integer type variations */
fprintf($fp, "<br><br>Testing fprintf() with integers:<br>");
foreach( $int_variation as $int_var ) {
fprintf( $fp, "\n\nIteration: %d\n",$counter);
foreach( $int_numbers as $int_num ) {
fprintf( $fp, "\n");
fprintf( $fp, $int_var, $int_num );
}
$counter++;
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>
EXERCISE
<?php
$int_numbers = [ 0, 1, -1, 2.7,
-2.7, 23333333,
-23333333, "1234" ];
/* creating dumping file */
$data_file = __DIR__ . '/fprintf05.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
/* binary type variations */
fprintf($fp, "<br><br>Testing fprintf() with binary:<br>");
foreach( $int_numbers as $bin_num ) {
fprintf( $fp, "\n<br>");
fprintf( $fp, "%b", $bin_num );
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>
EXERCISE
<?php
$char_variation = array( 'a', "a", 67, -67, 99 );
/* creating dumping file */
$data_file = __DIR__ . '/fprintf06.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
/* char type variations */
fprintf($fp, "<br><br>Testing fprintf() for chars:<br>");
foreach( $char_variation as $char ) {
fprintf( $fp, "\n<br>");
fprintf( $fp,"%c", $char );
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>
EXERCISE
<?php
$int_numbers = [ 0, 1, -1, 2.7,
-2.7, 23333333,
-23333333, "1234" ];
/* creating dumping file */
$data_file = __DIR__ . '/fprintf07.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
/* %e type variations */
fprintf($fp, "<br><br>Testing fprintf() for scientific type:<br>");
foreach( $int_numbers as $num ) {
fprintf( $fp, "\n<br>");
fprintf( $fp, "%e", $num );
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>
EXERCISE
<?php
$int_numbers = array( 0, 1, -1, 2.7,
-2.7, 23333333,
-23333333, "1234" );
/* creating dumping file */
$data_file = __DIR__ . '/fprintf08.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
/* unsigned int type variation */
fprintf($fp, "<br><br>Testing fprintf() for unsigned integers:<br>");
foreach( $int_numbers as $unsig_num ) {
fprintf( $fp, "\n<br>");
fprintf( $fp, "%u", $unsig_num );
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>
EXERCISE
<?php
$int_numbers = array( 0, 1, -1, 2.7,
-2.7, 23333333,
-23333333, "1234" );
/* creating dumping file */
$data_file = __DIR__ . '/fprintf09.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
/* octal type variations */
fprintf($fp, "<br><br>Testing fprintf() for octals:<br>");
foreach( $int_numbers as $octal_num ) {
fprintf( $fp, "\n<br>");
fprintf( $fp, "%o", $octal_num );
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>
EXERCISE
<?php
$int_variation = [ "%d", "%-d", "%+d",
"%7.2d", "%-7.2d",
"%07.2d", "%-07.2d",
"%'#7.2d" ];
$int_numbers = [ 0, 1, -1, 2.7,
-2.7, 23333333,
-23333333, "1234" ];
/* creating dumping file */
$data_file = __DIR__ . '/fprintf10.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
/* hexadecimal type variations */
fprintf($fp, "<br><br>Testing fprintf() for hexadecimals:<br>");
foreach( $int_numbers as $hexa_num ) {
fprintf( $fp, "<br>");
fprintf( $fp, "%x", $hexa_num );
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>
EXERCISE
<?php
$string_variation = array( "%5s", "%-5s", "%05s", "%'#5s" );
$strings = array( NULL, "abc", 'aaa' );
/* creating dumping file */
$data_file = __DIR__ . '/fprintf11.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
$counter = 1;
/* string type variations */
fprintf($fp, "<br><br>Testing fprintf() for string types:<br>");
foreach( $string_variation as $string_var ) {
fprintf( $fp, "<br><br>Iteration: %d<br>",$counter);
foreach( $strings as $str ) {
fprintf( $fp, "<br>");
fprintf( $fp, $string_var, $str );
}
$counter++;
}
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
// to erase the created $data_file
?>