vfprintfWRITES a formatted STRING to a stream.
Undue substitutions will produce unpredictable results.
<?php
int vfprintf ( res $handle , str $format , arr $args )
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 = ARRAY as an additional argument
?>
$handle
The file system pointer as a resourse.
Typically created by fopen.
$format
This function
vfprintf is SELF-SUFFICIENT, because it displays data without the need for ancillary functions.
Each SPECIFIER, present in FORMATTED STRING, must have its own SUBSTITUTE ARGUMENT.
Operates as
printf but accepts an ARRAY of arguments, rather than a variable number of arguments.
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 |
$args
Array of additional arguments.
EXERCISE
<?php
$file01 = __DIR__ . '/atom_layers.txt';
$layers01 = [ 2, 8, 18, 18, 32, 32, 18, 8 ];
$fp = fopen($file01, 'w');
if($fp == TRUE)
{
vfprintf($fp, 'K=%d, L=%d, M=%d, N=%d,
O=%d, P=%d, Q=%d, R=%d', $layers01);
fclose($fp);
echo 'The file WAS SAVED as expected.<br><br>';
echo file_get_contents($file01) . '<br><br>';
unlink($file01);
}
else
{
echo 'The file WAS NOT SAVED as expected.<br><br>';
}
?>
RESULT
The file WAS SAVED as expected.
File content, (atom_layers.txt):
K=2, L=8, M=18, N=18, O=32, P=32, Q=18, R=8
EXERCISE
<?php
function writeAndDump($fp, $format, $args)
{
ftruncate( $fp, 0 );
$length = vfprintf( $fp, $format, $args );
rewind( $fp );
$content = stream_get_contents( $fp );
echo '<br><br>Content:<br>' . $content;
echo '<br>Lenght: ';
echo $length;
echo '<br>';
}
echo "Testing vfprintf() :<br>
basic functionality.<br>";
// Open handle
$file = __DIR__ . '/vfprintf02.txt';
$fp = fopen( $file, "a+" );
// Test vfprintf()
writeAndDump( $fp, "Foo is %d and %s", array( 30, 'bar' ) );
writeAndDump( $fp, "%s %s %s", array( 'bar', 'bar', 'bar' ) );
writeAndDump( $fp, "%d digit", array( '54' ) );
writeAndDump( $fp, "%b %b", array( true, false ) );
writeAndDump( $fp, "%c %c %c", array( 65, 66, 67 ) );
writeAndDump( $fp, "%e %E %e", array( 1000, 2e4, +2e2 ) );
writeAndDump( $fp, "%02d", array( 50 ) );
writeAndDump( $fp, "Testing %b %d %f %s %x %X",
array( 9, 6, 2.5502, "foobar", 15, 65 ) );
// Close handle
fclose( $fp );
unlink( $file );
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
basic functionality - using string format.<br><br>";
// Initialise all required variables
$format = "format";
$format1 = "%s\n";
$format2 = "%s %s\n";
$format3 = "%s %s %s\n";
$arg1 = array("one");
$arg2 = array("one","two");
$arg3 = array("one","two","three");
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf03.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1, $arg1);
vfprintf($fp, $format2, $arg2);
vfprintf($fp, $format3, $arg3);
fclose($fp);
echo(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
basic functionality - using integer format<br><br>";
// Initialise all required variables
$format = "format";
$format1 = "%d";
$format2 = "%d %d";
$format3 = "%d %d %d";
$arg1 = array(111);
$arg2 = array(111,222);
$arg3 = array(111,222,333);
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf04.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1, $arg1);
fprintf($fp, "\n");
vfprintf($fp, $format2, $arg2);
fprintf($fp, "\n");
vfprintf($fp, $format3, $arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
basic functionality - using float format:<br><br>";
// Initialise all required variables
setlocale(LC_ALL, 'es-ES', 'es_ES');
$format = "format";
$format1 = "%f";
$format2 = "%f %f";
$format3 = "%f %f %f";
$format11 = "%F";
$format22 = "%F %F";
$format33 = "%F %F %F";
$arg1 = array(11.11);
$arg2 = array(11.11,22.22);
$arg3 = array(11.11,22.22,33.33);
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf05.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1,$arg1);
fprintf($fp, "\n");
vfprintf($fp,$format11,$arg1);
fprintf($fp, "\n");
vfprintf($fp,$format2,$arg2);
fprintf($fp, "\n");
vfprintf($fp,$format22,$arg2);
fprintf($fp, "\n");
vfprintf($fp,$format3,$arg3);
fprintf($fp, "\n");
vfprintf($fp, $format33,$arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
basic functionality - using bool format.<br><br>";
// Initialise all required variables
$format = "format";
$format1 = "%b";
$format2 = "%b %b";
$format3 = "%b %b %b";
$arg1 = array(TRUE);
$arg2 = array(TRUE,FALSE);
$arg3 = array(TRUE,FALSE,TRUE);
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf06.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1,$arg1);
fprintf($fp, "\n");
vfprintf($fp, $format2,$arg2);
fprintf($fp, "\n");
vfprintf($fp, $format3,$arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
basic functionality - using char format.<br><br>";
// Initialise all required variables
$format = "format";
$format1 = "%c";
$format2 = "%c %c";
$format3 = "%c %c %c";
$arg1 = array(65);
$arg2 = array(65,66);
$arg3 = array(65,66,67);
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf07.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1,$arg1);
fprintf($fp, "\n");
vfprintf($fp, $format2,$arg2);
fprintf($fp, "\n");
vfprintf($fp, $format3,$arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
basic functionality - using exponential format.<br><br>";
// Initialise all required variables
$format = "format";
$format1 = "%e";
$format2 = "%f %f";
$format3 = "%e %e %e";
$arg1 = array(1000);
$arg2 = array(1000,2000);
$arg3 = array(1000,2000,3000);
setlocale(LC_ALL, 'pt-PT', 'pt_PT');
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf08.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1,$arg1);
fprintf($fp, "\n");
vfprintf($fp, $format2,$arg2);
fprintf($fp, "\n");
vfprintf($fp, $format3,$arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
if (PHP_INT_SIZE != 8)
die("skip this test is for 64bit platform only");
echo "Testing vfprintf() :<br>
basic functionality - using unsigned format.<br><br>";
// Initialise all required variables
$format = "format";
$format1 = "%u";
$format2 = "%u %u";
$format3 = "%u %u %u";
$arg1 = array(-1111);
$arg2 = array(-1111,-1234567);
$arg3 = array(-1111,-1234567,-2345432);
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf09.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1,$arg1);
fprintf($fp, "\n");
vfprintf($fp, $format2,$arg2);
fprintf($fp, "\n");
vfprintf($fp, $format3,$arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
basic functionality - using octal format.<br><br>";
// Initialise all required variables
$format = "format";
$format1 = "%o";
$format2 = "%o %o";
$format3 = "%o %o %o";
$arg1 = array(021);
$arg2 = array(021,0347);
$arg3 = array(021,0347,0567);
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf10.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1,$arg1);
fprintf($fp, "\n");
vfprintf($fp, $format2,$arg2);
fprintf($fp, "\n");
vfprintf($fp, $format3,$arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf) :<br>
basic functionality - using hexadecimal format.<br><br>";
// Initialising different format strings
$format = "format";
$format1 = "%x";
$format2 = "%x %x";
$format3 = "%x %x %x";
$format11 = "%X";
$format22 = "%X %X";
$format33 = "%X %X %X";
$arg1 = array(11);
$arg2 = array(11,132);
$arg3 = array(11,132,177);
/* creating dumping file */
$data_file = __DIR__ . '/vfprintf_basic11.txt';
if (!($fp = fopen($data_file, 'wt')))
return;
vfprintf($fp, $format1, $arg1);
fprintf($fp, "\n");
vfprintf($fp, $format11, $arg1);
fprintf($fp, "\n");
vfprintf($fp, $format2, $arg2);
fprintf($fp, "\n");
vfprintf($fp, $format22, $arg2);
fprintf($fp, "\n");
vfprintf($fp, $format3, $arg3);
fprintf($fp, "\n");
vfprintf($fp, $format33, $arg3);
fprintf($fp, "\n");
fclose($fp);
print_r(file_get_contents($data_file));
unlink($data_file);
?>
EXERCISE
<?php
echo "Testing vfprintf() :<br>
variation functionality.<br><br>";
// Open handle
$file = __DIR__ . '/vfprintf12.txt';
$fp = fopen( $file, 'a+' );
$funset = fopen( __FILE__, 'r' );
unset( $funset );
class FooClass
{
public function __toString()
{
return "Object";
}
}
// Output facilitating function
function writeAndDump($fp, $format, $args)
{
try {
ftruncate( $fp, 0 );
$length = vfprintf( $fp, $format, $args );
rewind( $fp );
$content = stream_get_contents( $fp );
var_dump( $content );
var_dump( $length );
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}
}
// Test vfprintf()
writeAndDump( $fp, "format", null );
writeAndDump( $fp, "Foo is %d and %s", array( 30, 'bar' ) );
writeAndDump( $fp, "Foobar testing", array() );
writeAndDump( $fp, "%s %s %s", array( 'bar', 'bar', 'bar' ) );
writeAndDump( $fp, "%02d", array( 50 ) );
writeAndDump( $fp, "", array() );
writeAndDump( $fp, "Testing %b %d %f %o %s %x %X",
array( 9, 6, 2.5502, 24, "foobar", 15, 65 ) );
// Close handle
fclose( $fp );
unlink( $file );
?>
EXERCISE
<?php
// Open handle
$file = 'vfprintf13.txt';
$fp = fopen( $file, "a+" );
echo "<br><br>Testing vfprintf() function
with more than expected
number of arguments.<br><br>";
$format = 'string_val';
$args = array( 1, 2 );
$extra_arg = 10;
try {
var_dump( vfprintf( $fp, $format, $args, $extra_arg ) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
try {
var_dump( vfprintf( $fp, "Foo %d", array(6), "bar" ) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
// Close handle
fclose($fp);
unlink( $file );
?>
EXERCISE
<?php
// Open handle
$file = 'vfprintf14.txt';
$fp = fopen( $file, "a+" );
echo "Testing vfprintf() function
with wrong variable types as argument.<br><br>";
try {
vfprintf($fp, array( 'foo %d', 'bar %s' ), 3.55552);
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br>";
}
try {
vfprintf($fp, "Foo: %s", "not available");
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
try {
vfprintf($fp, "Foo %y fake", ["not available"]);
} catch (ValueError $e) {
echo $e->getMessage(), "<br>";
}
rewind( $fp );
var_dump( stream_get_contents( $fp ) );
ftruncate( $fp, 0 );
rewind( $fp );
// Close handle
fclose( $fp );
unlink( $file );
?>
EXERCISE
<?php
// Open handle
$file = 'vfprintf15.txt';
$fp = fopen( $file, "a+" );
echo "Testing vfprintf() function
with other strangeties.<br><br>";
try {
var_dump( vfprintf( 'foo', 'bar', array( 'baz' ) ) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
try {
var_dump( vfprintf( $fp, 'Foo %$c-0202Sd', array( 2 ) ) );
} catch(\ValueError $e) {
print('Error found: '.$e->getMessage()."<br>");
}
// Close handle
fclose( $fp );
unlink( $file );
?>