vprintfOUTPUTS a formatted STRING.
This function, like
printf, is
SELF-SUFFICIENT, because it displays data without the need for auxiliary functions.
Undue substitutions will produce unpredictable results.
<?php
int vprintf ( str $format , arr $args )
where,
$format = STRING FORMAT, with internal,
% replaceable specifiers
( SEE the below TABLE )
$args = SUBSTITUTE ARGUMENTS
?>
$formatOperates as
printf but accepts an
ARRAY of arguments, rather than a variable number of arguments.
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 |
$args
The substitute arguments.
EXERCISE
<?php
$strA = 'If everyone liked %s, what would be the %s?';
$arg1 = ['BLUE', 'YELLOW'];
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This function is AUTO SUFFICIENT
The displayed data is made directly
without the need for ancillary functions
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
vprintf($strA, $arg1);
echo '<br><br>';
?>
RESULT
If everyone liked BLUE , what would be the YELLOW ?
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ATTENTION FOR THE COMPATIBILITY WITH LOCALE
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
setlocale(LC_ALL, "pt-br", "pt_BR.utf-8");
$cus_pt = "Este produto tem custo de %s%1.2f por unidade.";
$curval_br = [ 'R$', 5.71 ];
vprintf($cus_pt, $curval_br);
echo '<br><br>';
setlocale(LC_ALL, "en-us", "en_US");
$cus_us = "This product costs %s%1.2f per unit.";
$curval_us = [ 'U$', 1.00 ];
vprintf($cus_us, $curval_us);
?>
RESULT
Este produto tem custo de R$5,71 por unidade.
This product costs U$1.00 per unit.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ATTENTION FOR THE NON COMPATIBILITY WITH LOCALE
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
setlocale(LC_ALL, "pt-br","pt_BR.utf-8");
$cus_pt = "This product costs %s%1.2F per unit.";
$curval_br = [ 'R$', 5.71 ];
vprintf($cus_pt, $curval_br);
echo '<br><br>';
setlocale(LC_ALL, "en-us", "en_US");
$cus_us = "This product costs %s%1.2f per unit.";
$curval_us = [ 'U$', 1.00 ];
vprintf($cus_us, $curval_us);
?>
RESULT
This product costs R$5.71 per unit.
This product costs U$1.00 per unit.
EXERCISE
<?php
setlocale(LC_ALL, "en_US", "en-US");
$str04n = <<<EOD
In PHP there are TWO types
of variables:
\n<br>INTERNAL: %s
and \n<br>OF USER: %s
EOD;
$arg4 = [ '(predefined) ', '(user-defined).'];
$res04n = vprintf($str04n, $arg4);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This function is AUTO SUFFICIENT
The displayed data is made directly
without the need for ancillary functions,
however, if you treat it as a VARIABLE and
use: echo or print to display it,
this will additionally show the
number of characters involved
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo '<br><br>' . $res04n . ' characters';
?>
RESULT
In PHP there are TWO types of variables:
INTERNAL: (predefined) and
OF USER: (user-defined).
110 characters
EXERCISE
<?php
$str05 = '2021 is equal to<br>
%o (octal)<br>
and to<br>%X (hexadecimal)';
$arg6 = [ 2021, 2021 ];
$res05 = vprintf($str05, $arg6);
echo '<br><br>' . $res05 . ' characters';
?>
RESULT
2021 is equal to
3745 (octal)
and to
7E5 (hexadecimal)
118 characters
EXERCISE
<?php
$arrus = [ "en-us", "en_US", "american",
"american-english", "english-american",
"english-us", "english-usa", "enu",
"us", "usa" ];
$arrbr = ["pt-br.utf-8", "pt_BR.utf-8",
"portuguese-brazil", "ptb" ];
setlocale(LC_ALL, $arrus);
$str06 = '2018.99987 + 421.9876 = %f';
$arg8 = [2018.99987 + 421.9876 ];
$res06 = vprintf($str06, $arg8);
echo '<br><br>' . $res06 . ' characters<br><br>';
setlocale(LC_ALL, $arrbr );
$res06 = vprintf($str06, $arg8);
echo '<br><br>' . $res06 . ' characters';
?>
RESULT
2018.99987 + 421.9876 = 2440.987470
35 characters
2018.99987 + 421.9876 = 2440,987470
35 characters
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality - using string format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%s";
$format2 = "%s %s";
$format3 = "%s %s %s";
$arg1 = array("one");
$arg2 = array("one","two");
$arg3 = array("one","two","three");
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality - using integer format.<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);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality - using float format.<br>";
// Initialise all required variables
$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);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format11,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format22,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
$result = vprintf($format33,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality - using bool format.<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);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality - using char format.<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);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality -
using exponential format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%e";
$format2 = "%e %e";
$format3 = "%e %e %e";
$arg1 = array(1000);
$arg2 = array(1000,2000);
$arg3 = array(1000,2000,3000);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality - using unsigned format.<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);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality - using octal format.<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);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
basic functionality -
using hexadecimal format.<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);
$result = vprintf($format1,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format11,$arg1);
echo "<br>";
var_dump($result);
$result = vprintf($format2,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format22,$arg2);
echo "<br>";
var_dump($result);
$result = vprintf($format3,$arg3);
echo "<br>";
var_dump($result);
$result = vprintf($format33,$arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
/*
* Test vprintf() when different
* unexpected format strings are passed to
* the '$format' argument of the function
*/
echo "Testing vprintf() :<br>
with unexpected values for format argument.<br>";
// initialising the required variables
$args = array(1, 2);
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// declaring a class
class sample
{
public function __toString() {
return "object";
}
}
// Defining resource
$file_handle = fopen(__FILE__, 'r');
//array of values to iterate over
$values = array(
// int data
/*1*/ 0,
1,
12345,
-2345,
// float data
/*5*/ 10.5,
-10.5,
10.1234567e10,
10.7654321E-10,
.5,
// array data
/*10*/ array(),
array(0),
array(1),
array(1,2),
array('color' => 'red', 'item' => 'pen'),
// null data
/*15*/ NULL,
null,
// boolean data
/*17*/ true,
false,
TRUE,
FALSE,
// empty data
/*21*/ "",
'',
// object data
/*23*/ new sample(),
// undefined data
/*24*/ @$undefined_var,
// unset data
/*25*/ @$unset_var,
// resource data
/*26*/ $file_handle
);
// loop through each element of the array for format
$counter = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $counter<br>";
try {
$result = vprintf($value, $args);
echo "<br>";
var_dump($result);
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br>";
}
$counter++;
}
// closing the resource
fclose($file_handle);
?>
EXERCISE
<?php
/*
* Test vprintf() when different
* unexpected values are passed to
* the '$args' arguments of the function
*/
echo "Testing vprintf() :<br>
with unexpected values for args argument.<br>";
// initialising the required variables
$format = '%s';
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// declaring a class
class sample
{
public function __toString() {
return "object";
}
}
// Defining resource
$file_handle = fopen(__FILE__, 'r');
//array of values to iterate over
$values = array(
// int data
/*1*/ 0,
1,
12345,
-2345,
// float data
/*5*/ 10.5,
-10.5,
10.1234567e10,
10.7654321E-10,
.5,
// null data
/*10*/ NULL,
null,
// boolean data
/*12*/ true,
false,
TRUE,
FALSE,
// empty data
/*16*/ "",
'',
// string data
/*18*/ "string",
'string',
// object data
/*20*/ new sample(),
// undefined data
/*21*/ @$undefined_var,
// unset data
/*22*/ @$unset_var,
// resource data
/*23*/ $file_handle
);
// loop through each element of
// the array for args
$counter = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $count<br>";
try {
$result = vprintf($format,$value);
echo "<br>";
var_dump($result);
} catch (\TypeError $e) {
echo $e->getMessage(), "<br>";
} catch (\ValueError $e) {
echo $e->getMessage(), "<br>";
}
$counter++;
};
// closing the resource
fclose($file_handle);
?>
EXERCISE
<?php
/*
* Test vprintf() when different
* int formats and int values are passed to
* the '$format' and '$args' arguments
* of the function
*/
echo "Testing vprintf() :<br>
int formats with int values.<br>";
// defining array of int formats
$formats = array(
"%d",
"%+d %-d",
"%ld %4d %-4d",
"%10.4d %-10.4d %04d %04.4d",
"%'#2d %'2d %'$2d %'_2d",
"%d %d %d %d",
"% %%d",
'%3$d %4$d %1$d %2$d'
);
// Arrays of int values for the format
// defined in $format.
// Each sub array contains int values
// which correspond to each format string
// in $format
$args_array = array(
array(0),
array(-1, 1),
array(2147483647, +2147483640, -2147483640),
array(123456, 12345678, -1234567, 1234567),
array(111, 2222, 333333, 44444444),
array(0x123b, 0xfAb, 0123, 012),
array(1234, -5678),
array(3, 4, 1, 2)
);
// looping to test vprintf() with different
// int formats from the above $format array
// and with int values from the
// above $args_array array
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different int formats
* and non-int values are passed to
* the '$format' and '$args' arguments
* of the function
*/
echo "Testing vprintf() :<br>
int formats and non-integer values.<br>";
// defining array of int formats
$formats =
'%d %+d %-d
%ld %4d %-4d
%10.4d %-10.4d %.4d %04.4d
%\'#2d %\'2d %\'$2d %\'_2d
%3$d %4$d %1$d %2$d';
// Arrays of non int values for the
// format defined in $format.
// Each sub array contains non int values
// which correspond to each format in $format
$args_array = array(
// array of float values
array(2.2, .2, 10.2,
123456.234, -1234.6789, +1234.6789,
2e10, +2e5, 4e3, 22e+6,
12345.780, 12.000000011111,
-12.00000111111, -123456.234,
3.33, +4.44, 1.11,-2.22 ),
// array of strings
array(" ", ' ', 'hello',
'123hello', '-123hello', '+123hello',
"\12345678hello", "-\12345678hello",
'0123456hello', 'h123456ello',
"1234hello", "hello\0world",
"NULL", "true", "3", "4", '1', '2'),
// different arrays
array( array(0), array(1, 2), array(-1, -1),
array("123"), array('-123'), array("-123"),
array(true), array(false), array(TRUE),
array(FALSE), array("123hello"),
array("1", "2"), array('123hello'),
array(12=>"12twelve"), array("3"),
array("4"), array("1"), array("2") ),
// array of boolean data
array( true, TRUE, false,
TRUE, FALSE, 1,
true, false, TRUE, FALSE,
0, 1, 1, 0,
1, TRUE, 0, FALSE),
);
// looping to test vprintf() with different
// int formats from the above $format array
// and with non-int values from the
// above $args_array array
$counter = 1;
foreach($args_array as $args) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($formats, $args);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different float formats
* and float values are passed to
* the '$format' and '$args' arguments of the function
*/
echo "Testing vprintf() :<br>
int formats with float values.<br>";
// defining array of float formats
$formats = array(
"%f",
"%+f %-f %F",
"%lf %4f %-4f",
"%10.4f %-10.4F %04f %04.4f",
"%'#2f %'2f %'$2f %'_2f",
"%f %f %f %f",
"% %%f",
'%3$f %4$f %1$f %2$f'
);
// Arrays of float values for the
// format defined in $format.
// Each sub array contains float values
// which correspond to each format string in $format
$args_array = array(
array(0.0),
array(-0.1, +0.1, +10.0000006),
array(2147483649, +2147483640, -2147483640),
array(2e5, 2e-5, -2e5, -2e-5),
array(0.2E5, -0.2e40, 0.2E-20, 0.2E+20),
array(0x123b, 0xfAb, 0123, 012),
array(1234.1234, -5678.5678),
array(3.33, 4.44, 1.11, 2.22)
);
// looping to test vprintf() with different
// float formats from the above $format array
// and with float values from the
// above $args_array array
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different
* float formats and non-float values
* are passed to the '$format'
* and '$args' arguments of the function
*/
echo "Testing vprintf() :<br>
float formats and non-float values.<br>";
// defining array of float formats
$formats =
'%f %+f %-f
%lf %4f %-4f
%10.4f %-10.4f %04f %04.4f
%\'#2f %\'2f %\'$2f %\'_2f
%3$f %4$f %1$f %2$f';
// Arrays of non float values for the
// format defined in $format.
// Each sub array contains non float values
// which correspond to each format in $format
$args_array = array(
// array of int values
array(2, -2, +2,
123456, -12346789, +12346789,
123200, +20000, -40000, 22212,
12345780, 1211111, -12111111,
-12345634, 3, +4, 1,-2 ),
// array of strings
array(" ", ' ', 'hello',
'123hello', '-123hello', '+123hello',
"\12345678hello", "-\12345678hello",
'0123456hello', 'h123456ello',
"1234hello", "hello\0world",
"NULL", "true", "3", "4", '1', '2'),
// different arrays
array( array(0), array(1, 2), array(-1, -1),
array("123"), array('-123'), array("-123"),
array(true), array(false), array(TRUE),
array(FALSE), array("123hello"),
array("1", "2"), array('123hello'),
array(12=>"12twelve"), array("3"),
array("4"), array("1"), array("2") ),
// array of boolean data
array( true, TRUE, false,
TRUE, FALSE, 1,
true, false, TRUE, FALSE,
0, 1, 1, 0,
1, TRUE, 0, FALSE),
);
// looping to test vprintf() with different
// float formats from the above $format array
// and with non-float values from the
// above $args_array array
$counter = 1;
foreach($args_array as $args) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($formats, $args);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different string
* formats and string values are passed to
* the '$format' and '$args' arguments
* of the function
*/
echo "Testing vprintf() :<br>
string formats with strings.<br>";
// defining different heredoc strings
$heredoc_string = <<<EOT
This is string defined
using heredoc.
EOT;
/* heredoc string with only numerics */
$heredoc_numeric_string = <<<EOT
123456 3993
4849 string
EOT;
/* empty heardoc string */
$heredoc_empty_string = <<<EOT
EOT;
// defining array of string formats
$formats = array(
"%s",
"%+s %-s",
"%ls %4s %-4s",
"%10.4s %-10.4s %04s %04.4s",
"%'#2s %'2s %'$2s %'_2s",
"%% %%s",
'%3$s %4$s %1$s %2$s'
);
// Arrays of string values for the
// format defined in $format.
// Each sub array contains string values
// which correspond to each format string in $format
$args_array = array(
array(" "),
array("hello\0world", "hello\0"),
array("@#$%&*", "\x55F", "\001"),
array("sunday", 'monday', "tuesday", 'wednesday'),
array($heredoc_string, "abcdef", $heredoc_numeric_string,
$heredoc_empty_string),
array("one", "two", 'three'),
array("three", 'four', 'one', "two")
);
// looping to test vprintf() with different string
// formats from the above $format array
// and with string from the above $args_array array
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different string formats
* and non-string values are passed to
* the '$format' and '$args' arguments of the function
*/
echo "Testing vprintf() :<br>
String formats and non-string values.<br>";
// defining array of string formats
$formats =
'%s %+s %-s
%ls %4s %-4s
%10.4s %-10.4s %04s %04.4s
%\'#2s %\'2s %\'$2s %\'_2s
%3$s %4$s %1$s %2$s';
// Arrays of non string values for the format
// defined in $format.
// Each sub array contains non string values
// which correspond to each format in $format
$args_array = array(
// array of float values
array(2.2, .2, 10.2,
123456.234, -1234.6789, +1234.6789,
2.1234567e10, +2.7654321e10, -2.7654321e10,
12345.780, 12.000000011111, -12.00000111111,
-123456.234, 3.33, +4.44, 1.11,-2.22 ),
// array of int values
array(2, -2, +2,
123456, -12346789, +12346789,
123200, +20000, -40000, 22212,
12345780, 1211111, -12111111, -12345634,
3, +4, 1,-2 ),
// different arrays
array( array(0), array(1, 2), array(-1, -1),
array("123"), array('-123'), array("-123"),
array(true), array(false), array(TRUE),
array(FALSE), array("123hello"),
array("1", "2"), array('123hello'),
array(12=>"12twelve"), array("3"),
array("4"), array("1"), array("2") ),
// array of boolean data
array( true, TRUE, false,
TRUE, FALSE, 1,
true, false, TRUE, FALSE,
0, 1, 1, 0,
1, TRUE, 0, FALSE),
);
// looping to test vprintf() with different
// string formats from the above $format array
// and with non-string values from the
// above $args_array array
$counter = 1;
foreach($args_array as $args) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($formats, $args);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/* Prototype :
string vprintf(string format, array args)
* Description:
Output a formatted string
* Source code:
ext/standard/formatted_print.c
*/
/*
* Test vprintf() when different char formats and
* non-char values are passed to
* the '$format' and '$args' arguments of the function
*/
echo "Testing vprintf() : char formats and non-char values.<br>";
// defining an array of various char formats
$formats =
'%c %+c %-c
%lc %4c %-4c
%10.4c %-10.4c %04c %04.4c
%\'10c %\'10c %\'$10c %\'_10c
%3$c %4$c %1$c %2$c';
// Arrays of non char values for the format
// defined in $format.
// Each sub array contains non char values
// which correspond to each format in $format
$args_array = array(
// array of float values
array(65.8, -65.8, +66.8,
93.2, 126.8, -126.49,
35.44, -35.68, 32.99, -32.00,
-61.51, 61.51, 50.49, -54.50,
83.33, +84.44, 81.11, 82.22),
// array of int values
array(65, -65, +66,
169, 126, -126,
35, -35, 32, -32,
-61, 61, 50, -54,
83, +84, 81, 82),
// array of strings
array(" ", ' ', 'hello',
'123hello', '-123hello', '+123hello',
"\12345678hello", "-\12345678hello",
'0123456hello', 'h123456ello',
"1234hello", "hello\0world", "NULL", "true",
"3", "4", '1', '2'),
// different arrays
array( array(0), array(1, 2), array(-1, -1),
array("123"), array('-123'), array("-123"),
array(true), array(false), array(TRUE),
array(FALSE), array("123hello"),
array("1", "2"), array('123hello'),
array(12=>"12twelve"), array("3"),
array("4"), array("1"), array("2") ),
// array of boolean data
array( true, TRUE, false,
TRUE, FALSE, 1,
true, false, TRUE, FALSE,
0, 1, 1, 0,
1, TRUE, 0, FALSE),
);
// looping to test vprintf() with different
// char formats from the above $format array
// and with non-char values from the
// above $args_array array
$counter = 1;
foreach($args_array as $args) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($formats, $args);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different octal formats
* and octal values are passed to
* the '$format' and '$args' arguments
* of the function
*/
echo "Testing vprintf() :<br>
octal formats with octal values.<br>";
// defining array of octal formats
$formats = array(
"%o",
"%+o %-o",
"%lo %4o %-4o",
"%10.4o %-10.4o %04o %04.4o",
"%'#2o %'2o %'$2o %'_2o",
"%o %o %o %o",
"%% %%o",
'%3$o %4$o %1$o %2$o'
);
// Arrays of octal values for the
// format defined in $format.
// Each sub array contains octal values
// which correspond to each format string in $format
$args_array = array(
array(00),
array(-01, 01),
array(-020000000000, 017777777777, -017777777777),
array(0123456, 01234567, -01234567, 01234567),
array(0111, 02222, -0333333, -044444444),
array(0x123b, 0xfAb, 0123, 012),
array(01234, 0567),
array(03, 04, 01, 02)
);
// looping to test vprintf() with different
// octal formats from the above $formats array
// and with octal values from the above $args_array array
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different octal formats
* and non-octal values are passed to
* the '$format' and '$args' arguments
* of the function
*/
echo "Testing vprintf() :<br>
octal formats and non-octal values.<br>";
// defining array of octal formats
$formats =
'%o %+o %-o
%lo %4o %-4o
%10.4o %-10.4o %.4o
%\'#2o %\'2o %\'$2o %\'_2o
%3$o %4$o %1$o %2$o';
// Arrays of non octal values for the
// format defined in $format.
// Each sub array contains non octal values
// which correspond to each format in $format
$args_array = array(
// array of float values
array(2.2, .2, 10.2,
123456.234, -1234.6789, +1234.6789,
2e10, +2e12, 22e+12,
12345.780, 12.000000011111,
-12.00000111111, -123456.234,
3.33, +4.44, 1.11,-2.22 ),
// array of int values
array(2, -2, +2,
123456, -12346789, +12346789,
123200, +20000, 22212,
12345780, 1211111, -12111111,
-12345634, 3, +4, 1,-2 ),
// array of strings
array(" ", ' ', 'hello',
'123hello', '-123hello', '+123hello',
"\12345678hello", "-\12345678hello", 'h123456ello',
"1234hello", "hello\0world", "NULL", "true",
"3", "4", '1', '2'),
// different arrays
array( array(0), array(1, 2), array(-1, -1),
array("123"), array('-123'), array("-123"),
array(true), array(false), array(FALSE),
array("123hello"), array("1", "2"),
array('123hello'), array(12=>"12twelve"),
array("3"), array("4"), array("1"), array("2") ),
// array of boolean data
array( true, TRUE, false,
TRUE, FALSE, 1,
true, false, TRUE,
0, 1, 1, 0,
1, TRUE, 0, FALSE),
);
// looping to test vprintf() with different octal
// formats from the above $format array
// and with non-octal values from the
// above $args_array array
$counter = 1;
foreach($args_array as $args) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($formats, $args);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different hexa formats
* and hexa values are passed to
* the '$format' and '$args' arguments
* of the function
*/
echo "Testing vprintf() :<br>
hexa formats with hexa values.<br>";
// defining array of different hexa formats
$formats = array(
"%x",
"%+x %-x %X",
"%lx %4x %-4x",
"%10.4x %-10.4x %04x %04.4x",
"%'#2x %'2x %'$2x %'_2x",
"%x %x %x %x",
"% %%x",
'%3$x %4$x %1$x %2$x'
);
// Arrays of hexa values for the format
// defined in $format.
// Each sub array contains hexa values
// which correspond to each
// format string in $format
$args_array = array(
array(0x0),
array(-0x1, 0x1, +0x22),
array(0x7FFFFFFF, +0x7000000, -0x80000000),
array(123456, 12345678, -1234567, 1234567),
array(1, 0x2222, 0333333, -0x44444444),
array(0x123b, 0xfAb, "0xaxz", 012),
array(0x1234, 0x34),
array(0x3, 0x4, 0x1, 0x2)
);
// looping to test vprintf() with different
// char octal from the above $format array
// and with octal values from the above
// $args_array array
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different unsigned
* formats and unsigned values
* are passed to the '$format' and
* '$args' arguments of the function
*/
echo "Testing vprintf() :<br>
unsigned formats and unsigned values.<br>";
// defining array of unsigned formats
$formats = array(
'%u %+u %-u',
'%lu %4u %-4u',
'%10.4u %-10.4u %.4u',
'%\'#2u %\'2u %\'$2u %\'_2u',
'%3$u %4$u %1$u %2$u'
);
// Arrays of unsigned values for
// the format defined in $format.
// Each sub array contains unsigned
// values which correspond to
// each format string in $format
$args_array = array(
array(1234567, 01234567, 0 ),
array(12345678900, 1234, 12345),
array("1234000", 10e20, 1.2e2),
array(1, 0, 00, "10_"),
array(3, 4, 1, 2)
);
// looping to test vprintf() with
// different unsigned formats from
// the above $format array
// and with signed and other types
// of values from the above
// $args_array array
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different unsigned
* formats and signed values and
* other types of values
* are passed to the '$format' and
* '$args' arguments of the function
*/
echo "Testing vprintf() :<br>
unsigned formats and signed &
other types of values.<br>";
// defining array of unsigned formats
$formats =
'%u %+u %-u
%lu %4u %-4u
%10.4u %-10.4u %.4u
%\'#2u %\'2u %\'$2u %\'_2u
%3$u %4$u %1$u %2$u';
// Arrays of signed and other type of values
// for the format defined in $format.
// Each sub array contains signed values
// which correspond to each format in $format
$args_array = array(
// array of float values
array(+2.2, +.2, +10.2,
+123456.234, +123456.234, +1234.6789,
+2e10, +2e12, +22e+12,
+12345.780, +12.000000011111, -12.00000111111,
-123456.234,
+3.33, +4.44, +1.11,-2.22 ),
// array of strings
array(" ", ' ', 'hello',
'123hello', '-123hello', '+123hello',
"\12345678hello", "-\12345678hello", 'h123456ello',
"1234hello", "hello\0world", "NULL", "true",
"3", "4", '1', '2'),
// different arrays
array( array(0), array(1, 2), array(-1, -1),
array("123"), array('-123'), array("-123"),
array(true), array(TRUE), array(FALSE),
array("123hello"), array("1", "2"), array('123hello'),
array(12=>"12twelve"),
array("3"), array("4"), array("1"), array("2") ),
// array of boolean data
array( true, TRUE, false,
TRUE, FALSE, 1,
true, TRUE, FALSE,
0, 1, 1, 0,
1, TRUE, 0, FALSE),
);
// looping to test vprintf() with different
// unsigned formats from the above
// $format array and with signed and
// other types of values from the
// above $args_array array
$counter = 1;
foreach($args_array as $args) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($formats, $args);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different
* scientific formats and scientific values
* are passed to the '$format' and '$args'
* arguments of the function
*/
echo "Testing vprintf() :<br>
scientific formats and scientific values.<br>";
// defining array of scientific formats
$formats = array(
'%e %+e %-e',
'%le %4e %-4e',
'%10.4e %-10.4e %.4e',
'%\'#20e %\'20e %\'$20e %\'_20e',
'%3$e %4$e %1$e %2$e'
);
// Arrays of scientific values for the
// format defined in $format.
// Each sub array contains scientific values
// which correspond to each format
// string in $format
$args_array = array(
array(0, 1e0, "10e2" ),
array(2.2e2, 1000e-2, 1000e7),
array(-22e12, 10e20, 1.2e2),
array(1e1, +1e2, -1e3, "1e2_"),
array(3e3, 4e3, 1e3, 2e3)
);
// looping to test vprintf() with different
// scientific formats from the above $format array
// and with signed and other types of values
// from the above $args_array array
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
/*
* Test vprintf() when different scientific
* formats and non-scientific values are passed to
* the '$format' and '$args' arguments of the function
*/
echo "Testing vprintf() :<br>
scientific formats and non-scientific values.<br>";
// defining array of non-scientific formats
$formats =
'%e %+e %-e
%le %4e %-4e
%10.4e %-10.4e %04e %04.4e
%\'#2e %\'2e %\'$2e %\'_2e
%3$e %4$e %1$e %2$e';
// Arrays of non scientific values
// for the format defined in $format.
// Each sub array contains non scientific
// values which correspond to each
// format in $format
$args_array = array(
// array of float values
array(2.2, .2, 10.2,
123456.234, -1234.6789, +1234.6789,
20.00, +212.2, -411000000000, 2212.000000000001,
12345.780, 12.000000011111, -12.00000111111,
-123456.234,
3.33, +4.44, 1.11,-2.22 ),
// array of strings
array(" ", ' ', 'hello',
'123hello', '-123hello', '+123hello',
"\12345678hello", "-\12345678hello",
'0123456hello', 'h123456ello',
"1234hello", "hello\0world", "NULL", "true",
"3", "4", '1', '2'),
// different arrays
array( array(0), array(1, 2), array(-1, -1),
array("123"), array('-123'), array("-123"),
array(true), array(false), array(TRUE), array(FALSE),
array("123hello"), array("1", "2"),
array('123hello'), array(12=>"12twelve"),
array("3"), array("4"), array("1"), array("2") ),
// array of boolean data
array( true, TRUE, false,
TRUE, FALSE, 1,
true, false, TRUE, FALSE,
0, 1, 1, 0,
1, TRUE, 0, FALSE),
);
// looping to test vprintf() with different
// scientific formats from the above $format array
// and with non-scientific values from the
// above $args_array array
$counter = 1;
foreach($args_array as $args) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($formats, $args);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>
EXERCISE
<?php
echo "Testing vprintf() :<br>
with white spaces in format strings.<br>";
// initializing the format array
$formats = array(
"% d % d % d",
"% f % f % f",
"% F % F % F",
"% b % b % b",
"% c % c % c",
"% e % e % e",
"% u % u % u",
"% o % o % o",
"% x % x % x",
"% X % X % X",
"% E % E % E"
);
// initializing the args array
$args_array = array(
array(111, 222, 333),
array(1.1, .2, -0.6),
array(1.12, -1.13, +0.23),
array(1, 2, 3),
array(65, 66, 67),
array(2e1, 2e-1, -2e1),
array(-11, +22, 33),
array(012, -023, +023),
array(0x11, -0x22, +0x33),
array(0x11, -0x22, +0x33),
array(2e1, 2e-1, -2e1)
);
$counter = 1;
foreach($formats as $format) {
echo "<br><br>Iteration: $count<br>";
$result = vprintf($format, $args_array[$counter-1]);
echo "<br>";
var_dump($result);
echo "<br>";
$counter++;
}
?>