printfOUTPUTS a formatted STRING.
This function is
SELF-SUFFICIENT, because it does not require auxiliary functions to display the result.
Undue substitutions will produce unpredictable results.
<?php
int printf ( str $format [, mix $arg1 [, ... , mix $argN ]] )
where,
$format = STRING FORMAT, with internal,
% replaceable specifiers
( SEE the below TABLE )
$arg1, ... , $argN = SUBSTITUTE ARGUMENTS
?>
$format
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
First substitute argument.
$arg2 ... $argN
Additional substitute arguments.
EXERCISE
<?php
$strA = 'If everyone liked %s, what would be the %s?';
$arg1 = 'BLUE';
$arg2 = 'YELLOW';
/* - - - - - - - - - - - - - - - - - - - - - - - -
This function is AUTO SUFFICIENT
The displayed data is made directly
without the need for ancillary functions
- - - - - - - - - - - - - - - - - - - - - - - - */
printf($strA, $arg1, $arg2);
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.utf-8", "pt_BR.utf-8", "portuguese-brazil", "ptb");
$cus_pt = "Este produto tem custo de %s%1.2f por unidade.";
$cur_br = 'R$';
$val_pt = 5.71;
printf($cus_pt, $cur_br, $val_pt);
echo '<br><br>';
setlocale(LC_ALL, "en-us", "en_US", "american", "american english", "american-english", "english-american", "english-us", "english-usa", "enu", "us", "usa");
$cus_us = "This product costs %s%1.2f per unit.";
$cur_us = 'U$';
$val_us = 1.00;
printf($cus_us, $cur_us, $val_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.utf-8", "pt_BR.utf-8", "portuguese-brazil", "ptb");
$cus_pt = "This product costs %s%1.2F per unit.";
$cur_br = 'R$';
$val_pt = 5.71;
printf($cus_pt, $cur_br, $val_pt);
echo '<br><br>';
setlocale(LC_ALL, "en-us", "en_US", "american", "american english", "american-english", "english-american", "english-us", "english-usa", "enu", "us", "usa");
$cus_us = "This product costs %s%1.2f per unit.";
$cur_us = 'U$';
$val_us = 1.00;
printf($cus_us, $cur_us, $val_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", "american", "american english", "american-english", "english-american", "english-us", "english-usa", "enu", "us", "usa");
$str04n = <<<EOD
In PHP there are TWO types
of variables:\n<br>
INTERNAL: %s
and \n<br>OF USER: %s
EOD;
$arg4 = '(predefined) ';
$arg5 = '(user-defined).';
$res04n = printf($str04n, $arg4, $arg5);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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<br>is equal to<br> %o (octal)<br>and to<br> %X (hexadecimal)';
$arg6 = 2021;
$arg7 = 2021;
$res05 = printf($str05, $arg6, $arg7);
echo '<br><br>' . $res05 . ' characters';
?>
RESULT
2021
is equal to
3745 (octal)
and to
7E5 (hexadecimal)
68 characters
EXERCISE
<?php
$arrus = ["en-us","en_US", "american", "american english",
"american-english", "english-american", "english-us",
"english-usa", "enu", "us", "usa"];
$arrbr = ["pt-br", "pt_BR.utf-8", "portuguese-brazil", "ptb"];
setlocale(LC_ALL, $arrus);
$str06 = '2018.99987 + 421.9876 = %f';
$arg8 = 2018.99987 + 421.9876;
$res06 = printf($str06, $arg8);
echo '<br><br>' . $res06 . ' characters<br><br><br>';
setlocale(LC_ALL, $arrbr);
$res06 = printf($str06, $arg8);
echo '<br><br>' . $res06 . ' caracteres';
?>
RESULT
2018.99987 + 421.9876 = 2440.987470
35 characters
2018.99987 + 421.9876 = 2440,987470
35 caracteres
EXERCISE
<?php
/* Various input arrays for different format types */
$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,
1.2345678e99, -1.2345678e99 ];
$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" ];
$char_variation = [ 'a', "a", 67, -67, 99 ];
$string_variation = [ "%5s", "%-5s", "%05s", "%'#5s" ];
$strings = [ NULL, "abc", 'aaa' ];
/* Checking warning messages */
/* Zero argument */
echo "<br><br>Output for zero argument:<br>";
try {
printf();
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
/* Number of arguments not matching
as specified in format field */
echo "<br><br>Output for insufficient number of arguments:<br>";
$string = "dingy%sflem%dwombat";
$nbr = 5;
$name = "voudras";
try {
printf("%d $string %s", $nbr, $name);
} catch (\ArgumentCountError $e) {
print('Error found: '.$e->getMessage());
}
/* Scalar argument */
echo "<br><br>Output for scalar argument:<br>";
printf(3);
/* NULL argument */
echo "<br><br>Output for NULL as argument:<br>";
printf(NULL);
/* Float type variations */
$counter = 1;
echo "<br><br> Output for float type.<br>";
echo "<br>Input Float numbers variation array is:<br>";
print_r($float_numbers);
foreach( $float_variation as $float_var )
{
echo "<br><br>Float Iteration: $counter<br>";
foreach( $float_numbers as $float_num )
{
echo "<br>";
printf( $float_var, $float_num );
}
$counter++;
}
/* Integer type variations */
$counter = 1;
echo "<br><br> Output for integer type.<br>";
echo "<br>Input Integer numbers variation array is:<br>";
print_r($int_numbers);
foreach( $int_variation as $int_var )
{
echo "<br><br>Integer Iteration: $counter<br>";
foreach( $int_numbers as $int_num )
{
echo "<br>";
printf( $int_var, $int_num );
}
$counter++;
}
/* Binary type variations */
echo "<br><br> Output for binary type.<br>";
echo "<br>Input numbers variation array is:<br>";
print_r($int_numbers);
foreach( $int_numbers as $bin_num )
{
echo "<br>";
printf( "%b", $bin_num );
}
/* Chararter type variations */
echo "<br><br> Output for char type.<br>";
echo "<br>Input Characters variation array is:<br>";
print_r($char_variation);
foreach( $char_variation as $char )
{
echo "<br>";
printf( "%c", $char );
}
/* Scientific type variations */
echo "<br><br> Output for scientific type.<br>";
echo "<br>Input numbers variation array is:<br>";
print_r($int_numbers);
foreach( $int_numbers as $num )
{
echo "<br>";
printf( "%e", $num );
}
/* Unsigned Integer type variation */
echo "<br><br> Output for unsigned integer type.<br>";
echo "<br>Input Integer numbers variation array is:<br>";
print_r($int_numbers);
foreach( $int_numbers as $unsig_num )
{
echo "<br>";
printf( "%u", $unsig_num );
}
/* Octal type variations */
echo "<br><br> Output for octal type.<br>";
echo "<br>Input numbers variation array is:<br>";
print_r($int_numbers);
foreach( $int_numbers as $octal_num )
{
echo "<br>";
printf( "%o", $octal_num );
}
/* Hexadecimal type variations */
echo "<br><br> Output for hexadecimal type.<br>";
echo "<br>Input numbers variation array is:<br>";
print_r($int_numbers);
foreach( $int_numbers as $hexa_num )
{
echo "<br>";
printf( "%x", $hexa_num );
}
/* String type variations */
echo "<br><br> Output for string type.<br>";
echo "<br>Input Strings format variation array is:<br>";
print_r($string_variation);
echo "<br><br>Input strings variation array is:<br>";
print_r($strings);
foreach( $string_variation as $string_var )
{
foreach( $strings as $str )
{
echo "<br>";
printf( $string_var, $str );
}
}
/* variations of %g type */
$format_g = ["%g", "%.0g", "%+g", "%-g", "%-1.2g", "%+1.2g", "%G", "%.0G", "%+G", "%-G", "%-1.2G", "%+1.2G" ];
echo "<br><br> Output for '%g' type.<br>";
echo "<br>Input format variation array is:<br>";
print_r($format_g);
foreach( $format_g as $formatg )
{
printf("\n$formatg",123456);
printf("\n$formatg",-123456);
}
/* Some more typical cases */
$tempnum = 12345;
$tempstring = "abcdefghjklmnpqrstuvwxyz";
echo"<br><br> Output for '%%%.2f' as the
format parameter.<br>";
printf("%%%.2f",1.23456789e10);
echo"<br><br> Output for '%%' as the
format parameter.<br>";
printf("%%",1.23456789e10);
echo"<br><br> Output for precision
value more than maximum:<br>";
printf("%.988f",1.23456789e10);
echo"<br><br> Output for invalid width(-15) specifier:<br>";
try {
printf("%030.-15s", $tempstring);
} catch (ValueError $e) {
echo $e->getMessage();
}
echo"<br><br> Output for '%F' as the format parameter:<br>";
printf("%F",1.23456789e10);
echo"<br><br> Output for '%X' as the format parameter:<br>";
printf("%X",12);
echo"<br><br> Output with no format parameter:<br>";
printf($tempnum);
echo"<br><br> Output for multiple format parameters:<br>";
printf("%d %s %d\n", $tempnum, $tempstring, $tempnum);
echo"<br><br> Output for excess of mixed type arguments:<br>";
printf("%s", $tempstring, $tempstring, $tempstring);
echo"<br><br> Output for string format
parameter and integer type argument:<br>";
printf("%s", $tempnum);
echo"<br><br> Output for integer format
parameter and string type argument:<br>";
printf("%d", $tempstring);
?>
EXERCISE
<?php
echo "Testing printf() :
basic functionality - using string format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%s";
$format2 = "%s %s";
$format3 = "%s %s %s";
$arg1 = "arg1 argument";
$arg2 = "arg2 argument";
$arg3 = "arg3 argument";
echo "<br><br>Calling printf() with no arguments:<br>";
$result = printf($format);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with one arguments:<br>";
$result = printf($format1, $arg1);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with two arguments:<br>";
$result = printf($format2, $arg1, $arg2);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with string three arguments:<br>";
$result = printf($format3, $arg1, $arg2, $arg3);
echo "<br><br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing printf() :
basic functionality - using integer format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%d";
$format2 = "%d %d";
$format3 = "%d %d %d";
$arg1 = 111;
$arg2 = 222;
$arg3 = 333;
echo "<br><br>Calling printf() with no arguments:<br>";
$result = printf($format);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with one arguments:<br>";
$result = printf($format1, $arg1);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with two arguments:<br>";
$result = printf($format2, $arg1, $arg2);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with three arguments:<br>";
$result = printf($format3, $arg1, $arg2, $arg3);
echo "<br><br>";
var_dump($result);
?>
EXERCISE
<?php
echo " Testing printf() :
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 = 11.11;
$arg2 = 22.22;
$arg3 = 33.33;
echo "<br><br>Calling printf()
with no arguments:<br>";
$result = printf($format);
echo "<br>";
var_dump($result);
echo "<br><br>Calling printf()
with one arguments:<br>";
$result = printf($format1, $arg1);
echo "<br>";
var_dump($result);
$result = printf($format11, $arg1);
echo "<br>";
var_dump($result);
echo "<br><br>Calling printf()
with two arguments:<br>";
$result = printf($format2, $arg1, $arg2);
echo "<br>";
var_dump($result);
$result = printf($format22, $arg1, $arg2);
echo "<br>";
var_dump($result);
echo "<br><br>Calling printf()
with three arguments:<br>";
$result = printf($format3, $arg1, $arg2, $arg3);
echo "<br>";
var_dump($result);
$result = printf($format33, $arg1, $arg2, $arg3);
echo "<br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing printf() :
basic functionality - using bool format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%b";
$format2 = "%b %b";
$format3 = "%b %b %b";
$arg1 = TRUE;
$arg2 = FALSE;
$arg3 = true;
echo "<br><br>Calling printf() with no arguments:<br>";
$result = printf($format);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with one arguments:<br>";
$result = printf($format1, $arg1);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with two arguments:<br>";
$result = printf($format2, $arg1, $arg2);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with three arguments:<br>";
$result = printf($format3, $arg1, $arg2, $arg3);
echo "<br><br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing printf() :
basic functionality - using char format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%c";
$format2 = "%c %c";
$format3 = "%c %c %c";
$arg1 = 65;
$arg2 = 66;
$arg3 = 67;
echo "<br><br> Calling printf() with no arguments:<br>";
$result = printf($format);
echo "<br><br>";
var_dump($result);
echo "<br><br> Calling printf() with one arguments:<br>";
$result = printf($format1, $arg1);
echo "<br><br>";
var_dump($result);
echo "<br><br> Calling printf() with two arguments:<br>";
$result = printf($format2, $arg1, $arg2);
echo "<br><br>";
var_dump($result);
echo "<br><br> Calling printf() with three arguments:<br>";
$result = printf($format3, $arg1, $arg2, $arg3);
echo "<br><br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing printf() :
basic functionality - using exponential format.<br>";
// Initialise all required variables
$format = "format";
$format1 = "%e";
$format2 = "%E %e";
$format3 = "%e %E %e";
$arg1 = 1000;
$arg2 = 2e3;
$arg3 = +3e3;
echo "<br><br>Calling printf() with no arguments:<br>";
$result = printf($format);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with one argument:<br>";
$result = printf($format1, $arg1);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with two arguments:<br>";
$result = printf($format2, $arg1, $arg2);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with three arguments:<br>";
$result = printf($format3, $arg1, $arg2, $arg3);
echo "<br><br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing printf() :
basic functionality - using hexadecimal format.<br>";
// Initialise all required variables
// 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 = 11;
$arg2 = 132;
$arg3 = 177;
echo "<br><br>Calling printf() with no arguments:<br>";
$result = printf($format);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with one arguments:<br>";
$result = printf($format1, $arg1);
echo "<br><br>";
var_dump($result);
$result = printf($format11, $arg1);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with two arguments:<br>";
$result = printf($format2, $arg1, $arg2);
echo "<br><br>";
var_dump($result);
$result = printf($format22, $arg1, $arg2);
echo "<br><br>";
var_dump($result);
echo "<br><br>Calling printf() with three arguments:<br>";
$result = printf($format3, $arg1, $arg2, $arg3);
echo "<br><br>";
var_dump($result);
$result = printf($format33, $arg1, $arg2, $arg3);
echo "<br><br>";
var_dump($result);
?>
EXERCISE
<?php
echo "Testing printf() : error conditions.<br>";
// Zero arguments
echo "<br><br>Testing printf() function
with Zero arguments:<br>";
try {
var_dump( printf() );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
echo "<br><br>Testing printf() function
with less than expected
number of arguments:<br>";
$format1 = '%s';
$format2 = '%s%s';
$format3 = '%s%s%s';
$arg1 = 'one';
$arg2 = 'two';
echo "<br><br>Call printf
with one argument less than expected:<br>";
try {
var_dump( printf($format1) );
} catch (\ArgumentCountError $e) {
echo $e->getMessage(), "<br>";
}
try {
var_dump( printf($format2,$arg1) );
} catch (\ArgumentCountError $e) {
echo $e->getMessage(), "<br>";
}
try {
var_dump( printf($format3,$arg1,$arg2) );
} catch (\ArgumentCountError $e) {
echo $e->getMessage(), "<br>";
}
echo "<br><br>Call printf
with two argument less than expected:<br>";
try {
var_dump( printf($format2) );
} catch (\ArgumentCountError $e) {
echo $e->getMessage(), "<br>";
}
try {
var_dump( printf($format3,$arg1) );
} catch (\ArgumentCountError $e) {
echo $e->getMessage(), "<br>";
}
echo "<br><br>Call printf
with three argument less than expected:<br>";
try {
var_dump( printf($format3) );
} catch (\ArgumentCountError $e) {
echo $e->getMessage(), "<br>";
}
?>
EXERCISE
<?php
setlocale(LC_ALL, "pt-br", "pt_BR.utf-8", "portuguese-brazil", "ptb");
$f = 1.25;
echo '<pre>';
printf("%g %G %h %H\n", $f, $f, $f, $f);
echo '</pre><br>';
$f = 0.00000125;
echo '<pre>';
printf("%g %G %h %H\n", $f, $f, $f, $f);
echo '</pre>';
?>
EXERCISE
<?php
/*
* Testing printf() : with different unexpected
* values for format argument other than the strings
*/
echo "Testing printf() :
with unexpected values for format argument.<br>";
// initialing required variables
$arg1 = "second arg";
$arg2 = "third arg";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// declaring class
class sample
{
public function __toString() {
return "Object";
}
}
// creating a file 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
$count = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $count<br>";
// with default argument
try {
$result = printf($value);
echo "<br>";
var_dump($result);
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br>";
}
// with two arguments
try {
$result = printf($value, $arg1);
echo "<br>";
var_dump($result);
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br>";
}
// with three arguments
try {
$result = printf($value, $arg1, $arg2);
echo "<br>";
var_dump($result);
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br>";
}
$count++;
};
// close the resource
fclose($file_handle);
?>
EXERCISE
<?php
echo "Testing printf() :
with different types of values passed
for arg1 argument.<br>";
// initialing required variables
$format = '%s';
$arg2 = 'third argument';
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// declaring class
class sample
{
public function __toString() {
return "Object";
}
}
// creating a file 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*/ "",
'',
// string data
/*23*/ "string",
'string',
// object data
/*25*/ new sample(),
// undefined data
/*26*/ @$undefined_var,
// unset data
/*27*/ @$unset_var,
// resource data
/*28*/ $file_handle
);
// loop through each element of the array for arg1
$count = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $count<br>";
// with two arguments
$result = printf($format, $value);
echo "<br>";
var_dump($result);
// with three arguments
$result = printf($format, $value, $arg2);
echo "<br>";
var_dump($result);
$count++;
};
// closing the resource
fclose($file_handle);
?>
EXERCISE
<?php
/* Attention to differences in results
* in PHP versions 7.4.XX and 8.0.XX.
*/
setlocale(LC_ALL, 'pt-PT', 'pt_PT');
$f = 1.23456789012345678;
$fx = 1.23456789012345678e100;
var_dump($f, $fx);
echo "<br><br><br>";
printf("%.*f\n", 10, $f);
echo "<br><br>";
printf("%.*G\n", 10, $f);
echo "<br><br>";
printf("%.*g\n", -1, $fx);
echo "<br><br>";
printf("%.*G\n", -1, $fx);
echo "<br><br>";
printf("%.*h\n", -1, $fx);
echo "<br><br>";
printf("%.*H\n", -1, $fx);
echo "<br><br>";
printf("%.*s\n", 3, "foobar");
echo "<br><br><br>";
printf("%*f\n", 10, $f);
echo "<br><br>";
printf("%*G\n", 10, $f);
echo "<br><br>";
printf("%*s\n", 10, "foobar");
echo "<br><br><br>";
printf("%*.*f\n", 10, 3, $f);
echo "<br><br>";
printf("%*.*G\n", 10, 3, $f);
echo "<br><br>";
printf("%*.*s\n", 10, 3, "foobar");
echo "<br><br><br>";
printf("%1$.*2\$f\n", $f, 10);
echo "<br><br>";
printf("%.*2\$f\n", $f, 10);
echo "<br><br>";
printf("%2$.*f\n", 10, $f);
echo "<br><br>";
printf("%1$*2\$f\n", $f, 10);
echo "<br><br>";
printf("%*2\$f\n", $f, 10);
echo "<br><br>";
printf("%2$*f\n", 10, $f);
echo "<br><br>";
printf("%1$*2$.*3\$f\n", $f, 10, 3);
echo "<br><br>";
printf("%*2$.*3\$f\n", $f, 10, 3);
echo "<br><br>";
printf("%3$*.*f\n", 10, 3, $f);
echo "<br><br><br>";
try {
printf("%.*G\n", "foo", 1.5);
} catch (ValueError $e) {
echo $e->getMessage(), "<br>";
}
try {
printf("%.*G\n", -100, 1.5);
} catch (ValueError $e) {
echo $e->getMessage(), "<br>";
}
try {
printf("%.*s\n", -1, "Foo");
} catch (ValueError $e) {
echo $e->getMessage(), "<br>";
}
try {
printf("%*G\n", -1, $f);
} catch (ValueError $e) {
echo $e->getMessage(), "<br>";
}
?>