sprintf


string apg

RETURNS a formatted STRING.

This function is NOT SELF-SUFFICIENT because it requires the use of auxiliary functions to display results.





Undue substitutions will produce unpredictable results.



<?php

int sprintf 
( str $format [, mix $arg1 [, ... , mix $argN ]] )


where,

$format = STRING FORMAT, with internal, 
                                  % 
replaceable specifiers
                
( SEE the below TABLE )

$arg1, ... , $argN = SUBSTITUTE ARGUMENTS

?>

$format


As printf, 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.



  1 EXERCISE   

<?php

$strA 
= 'If everyone liked %s, what would be the %s?';

$arg1 = 'BLUE';
$arg2 = 'YELLOW';

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   This function is NOT AUTO SUFFICIENT
   
   The displayed data is made 
   with the need for ancillary functions
   like: echo or print
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

echo sprintf($strA, $arg1, $arg2) . '<br><br>';

?> 

 RESULT   

If everyone liked  BLUE , what would be the  YELLOW ?


  2 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 custa %s%1.2f por unidade.";
$cur_br = 'R$';
$val_pt = 5.71;
print 
sprintf($cus_pt, $cur_br, $val_pt);

echo 
'<br><br>';

setlocale(LC_ALL, "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;
print 
sprintf($cus_us, $cur_us, $val_us);

?> 

 RESULT   

Este produto custa R$5,71 por unidade.

This product costs U$1.00 per unit.



  3 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;
echo 
sprintf($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;
echo 
sprintf($cus_us, $cur_us, $val_us);

?> 

 RESULT   

This product costs R$5.71 per unit.

This product costs U$1.00 per unit.



  4 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 = sprintf($str04n, $arg4, $arg5);

/* - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - -
   This function is NOT AUTO SUFFICIENT
   
   The displayed data is made directly
   with the need for ancillary functions,
   however, if you treat it as a VARIABLE and
   you must use: echo or print to display it,
   instead of displaying the 
   number of characters involved
   - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - */

echo '<br><br>' . $res04n; 

?> 

 RESULT   

In PHP there are TWO types of variables:
INTERNAL: (predefined) and
OF USER: (user-defined).


  5 EXERCISE   

<?php

$str05 
= '2021 is equal to<br>%o (octal)<br>and to<br>%X (hexadecimal)';

$arg6 = 2021;
$arg7 = 2021;

$res05 = sprintf($str05, $arg6, $arg7);

echo 
$res05 . '<br><br>'; 

?> 

 RESULT   

2021 is equal to
3745 (octal)
and to
7E5 (hexadecimal)


  6 EXERCISE   

<?php

setlocale
(LC_ALL, "en-us", "en_US"); 

$str06 = '2018.99987 + 421.9876 = %f';

$arg8 = 2018.99987 + 421.9876;

$res06 = sprintf($str06, $arg8);

echo 
'<br><br>' . $res06; 

setlocale(LC_ALL, "pt-br.utf-8", "pt_BR.utf-8");

$res06 = sprintf($str06, $arg8);

echo 
'<br><br>' . $res06 . '<br><br>'; 

?> 

 RESULT   

2018.99987 + 421.9876 = 2440.987470

2018.99987 + 421.9876 = 2440,987470



  7 EXERCISE   

<?php

echo "Testing sprintf() :<br> 
           basic functionality - using string format.<br><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";

// Calling sprintf() with default arguments
print( sprintf($format) );
echo 
'<br><br>';
// Calling sprintf() with two arguments
print( sprintf($format1, $arg1) );
echo 
'<br><br>';
// Calling sprintf() with three arguments
print( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
// Calling sprintf() with four arguments
print( sprintf($format3, $arg1, $arg2, $arg3) );

?>

  8 EXERCISE   

<?php

echo "Testing sprintf() :<br>
           basic functionality - using integer format.<br><br>"
;

// Initialise all required variables
$format = "format";
$format1 = "Number: %d.";
$format2 = "Numbers: %d and %d.";
$format3 = "Numbers: %d, %d and %d.";
$arg1 = 111;
$arg2 = 222;
$arg3 = 333;

// Calling sprintf() with default arguments
echo sprintf($format);
echo 
'<br><br>';
// Calling sprintf() with two arguments
echo sprintf($format1, $arg1);
echo 
'<br><br>';
// Calling sprintf() with three arguments
echo sprintf($format2, $arg1, $arg2);
echo 
'<br><br>';
// Calling sprintf() with four arguments
echo sprintf($format3, $arg1, $arg2, $arg3);

?>

  9 EXERCISE   

<?php

echo "Testing sprintf() :<br>
           basic functionality - using float format.<br><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;

setlocale(LC_ALL, 'pt_PT', 'pt-PT');

// Calling sprintf() with default arguments
var_dump( sprintf($format) );
echo 
'<br><br><br>';

// Calling sprintf() with two arguments
var_dump( sprintf($format1, $arg1) );
echo 
'<br><br>';
var_dump( sprintf($format11, $arg1) );
echo 
'<br><br><br>';
// Calling sprintf() with three arguments
var_dump( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
var_dump( sprintf($format22, $arg1, $arg2) );
echo 
'<br><br><br>';
// Calling sprintf() with four arguments
var_dump( sprintf($format3, $arg1, $arg2, $arg3) );
echo 
'<br><br>';
var_dump( sprintf($format33, $arg1, $arg2, $arg3) );

?>

  10 EXERCISE   

<?php

echo "Testing sprintf() :<br>
          basic functionality - using bool format.<br><br>"
;

// Initialise all required variables
$format = "format";
$format1 = "TRUE = %b.";
$format2 = "TRUE = %b; FALSE = %b.";
$format3 = "TRUE = %b; FALSE = %b; true = %b.";
$arg1 = TRUE;
$arg2 = FALSE;
$arg3 = true;

// Calling sprintf() with default arguments
var_dump( sprintf($format) );
echo 
'<br><br>';
// Calling sprintf() with two arguments
var_dump( sprintf($format1, $arg1) );
echo 
'<br><br>';
// Calling sprintf() with three arguments
var_dump( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
// Calling sprintf() with four arguments
var_dump( sprintf($format3, $arg1, $arg2, $arg3) );

?>

  11 EXERCISE   

<?php

echo "Testing sprintf() :<br>
                 basic functionality - using char format.<br><br>"
;

// Initialise all required variables
$format = "format";
$format1 = "%c";
$format2 = "%c %c";
$format3 = "%c %c %c";
$arg1 = 65;
$arg2 = 66;
$arg3 = 67;

// Calling sprintf() with default arguments
var_dump( sprintf($format) );
echo 
'<br><br>';
// Calling sprintf() with two arguments
var_dump( sprintf($format1, $arg1) );
echo 
'<br><br>';
// Calling sprintf() with three arguments
var_dump( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
// Calling sprintf() with four arguments
var_dump( sprintf($format3, $arg1, $arg2, $arg3) );

?>

  12 EXERCISE   

<?php

echo "Testing sprintf() :<br>
                 basic functionality - using exponential format.<br><br>"
;

// Initialise all required variables
$format = "format";
$format1 = "%e";
$format2 = "%E %e";
$format3 = "%e %E %e";
$arg1 = 1000;
$arg2 = 2e3;
$arg3 = +3e3;

// Calling sprintf() with default arguments
var_dump( sprintf($format) );
echo 
'<br><br>';
// Calling sprintf() with two arguments
var_dump( sprintf($format1, $arg1) );
echo 
'<br><br>';
// Calling sprintf() with three arguments
var_dump( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
// Calling sprintf() with four arguments
var_dump( sprintf($format3, $arg1, $arg2, $arg3) );

?>

  13 EXERCISE   

<?php

echo "Testing sprintf() :<br>
                 basic functionality - using unsigned format.<br><br>"
;

// Initialise all required variables
$format = "format";
$format1 = "%u";
$format2 = "%u %u";
$format3 = "%u %u %u";
$arg1 = -1111;
$arg2 = -1234567;
$arg3 = +2345432;

// Calling sprintf() with default arguments
var_dump( sprintf($format) );
echo 
'<br><br>';
// Calling sprintf() with two arguments
var_dump( sprintf($format1, $arg1) );
echo 
'<br><br>';
// Calling sprintf() with three arguments
var_dump( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
// Calling sprintf() with four arguments
var_dump( sprintf($format3, $arg1, $arg2, $arg3) );

?>

  14 EXERCISE   

<?php

echo "Testing sprintf() :<br>
                 basic functionality - using octal format.<br><br>"
;

// Initialise all required variables
$format = "format";
$format1 = "%o";
$format2 = "%o %o";
$format3 = "%o %o %o";
$arg1 = 021;
$arg2 = -0347;
$arg3 = 0567;

// Calling sprintf() with default arguments
var_dump( sprintf($format) );
echo 
'<br><br>';
// Calling sprintf() with two arguments
var_dump( sprintf($format1, $arg1) );
echo 
'<br><br>';
// Calling sprintf() with three arguments
var_dump( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
// Calling sprintf() with four arguments
var_dump( sprintf($format3, $arg1, $arg2, $arg3) );

?>

  15 EXERCISE   

<?php
echo "Testing sprintf() :<br>
        basic functionality - using hexadecimal format.<br><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;

// Calling sprintf() with default arguments
var_dump( sprintf($format) );
echo 
'<br><br>';
// Calling sprintf() with two arguments
var_dump( sprintf($format1, $arg1) );
echo 
'<br><br>';
var_dump( sprintf($format11, $arg1) );
echo 
'<br><br>';
// Calling sprintf() with three arguments
var_dump( sprintf($format2, $arg1, $arg2) );
echo 
'<br><br>';
var_dump( sprintf($format22, $arg1, $arg2) );
echo 
'<br><br>';
// Calling sprintf() with four arguments
var_dump( sprintf($format3, $arg1, $arg2, $arg3) );
echo 
'<br><br>';
var_dump( sprintf($format33, $arg1, $arg2, $arg3) );

?>

  16 EXERCISE   

<?php

var_dump
(sprintf("%3.2f", 1.2));
echo 
"<br><br>";
var_dump(sprintf("%-3.2f", 1.2));
echo 
"<br><br>";
var_dump(sprintf("%03.2f", 1.2));
echo 
"<br><br>";
var_dump(sprintf("%-03.2f", 1.2));
echo 
"<br><br><br>";
var_dump(sprintf("%5.2f", 3.4));
echo 
"<br><br>";
var_dump(sprintf("%-5.2f", 3.4));
echo 
"<br><br>";
var_dump(sprintf("%05.2f", 3.4));
echo 
"<br><br>";
var_dump(sprintf("%-05.2f", 3.4));
echo 
"<br><br><br>";
var_dump(sprintf("%7.2f", -5.6));
echo 
"<br><br>";
var_dump(sprintf("%-7.2f", -5.6));
echo 
"<br><br>";
var_dump(sprintf("%07.2f", -5.6));
echo 
"<br><br>";
var_dump(sprintf("%-07.2f", -5.6));
echo 
"<br><br><br>";
var_dump(sprintf("%3.4f", 1.2345678e99));

?>

  17 EXERCISE   

<?php

var_dump
(sprintf("%.3F", 100.426));
echo 
'<br><br>';
var_dump(sprintf("%.2F", 100.426));
echo 
'<br><br>';
var_dump(sprintf("%d",   100.426));
echo 
'<br><br>';
var_dump(sprintf("%d",   100.9));
echo 
'<br><br>';
var_dump(sprintf("%o",   100.426));
echo 
'<br><br>';
var_dump(sprintf("%o",   100.9));
echo 
'<br><br><br>';

/* copy & paste from the docs */

/* example#1: Argument swapping */
$num = 100.1;
$location = "world";

$format = 'There are %d monkeys in the %s';
var_dump(sprintf($format, $num, $location));
echo 
'<br><br><br>';
/* example#2: Argument swapping */
$format = 'The %s contains %d monkeys';
var_dump(sprintf($format, $num, $location));
echo 
'<br><br><br>';
/* example#3: Argument swapping */
$format = 'The %2$s contains %1$d monkeys';
var_dump(sprintf($format, $num, $location));
echo 
'<br><br><br>';
/* example#4: Argument swapping */
$format = 'The %2$s contains %1$d monkeys.
    That\'s a nice %2$s full of %1$d monkeys.'
;
var_dump(sprintf($format, $num, $location));
echo 
'<br><br><br>';
/* example#5: various examples */
$n =  43951789;
$u = -43951789;
$c = 65; // ASCII 65 is 'A'

// notice the double %%, this prints a literal '%' character
var_dump(sprintf("%%b = '%b'", $n)); 
// binary representation
echo '<br><br>';
var_dump(sprintf("%%c = '%c'", $c)); 
// print the ascii character, same as chr() function
echo '<br><br>';
var_dump(sprintf("%%d = '%d'", $n)); 
// standard integer representation
echo '<br><br>';
var_dump(sprintf("%%e = '%e'", $n)); 
// scientific notation
echo '<br><br>';
var_dump(sprintf("%%u = '%u'", $n)); 
// unsigned integer representation of a positive integer
echo '<br><br>';
var_dump(sprintf("%%u = '%u'", $u)); 
// unsigned integer representation of a negative integer
echo '<br><br>';
var_dump(sprintf("%%f = '%f'", $n)); 
// floating point representation
echo '<br><br>';
var_dump(sprintf("%%o = '%o'", $n)); 
// octal representation
echo '<br><br>';
var_dump(sprintf("%%s = '%s'", $n)); 
// string representation
echo '<br><br>';
var_dump(sprintf("%%x = '%x'", $n)); 
// hexadecimal representation (lower-case)
echo '<br><br>';
var_dump(sprintf("%%X = '%X'", $n)); 
// hexadecimal representation (upper-case)
echo '<br><br><br>';
var_dump(sprintf("%%+d = '%+d'", $n));
 echo 
'<br><br><br>';
// sign specifier on a positive integer
var_dump(sprintf("%%+d = '%+d'", $u)); 
// sign specifier on a negative integer
echo '<br><br><br>';

/* example#6: string specifiers */
$s = 'monkey';
$t = 'many monkeys';

var_dump(sprintf("[%s]",      $s)); 
// standard string output
echo '<br><br>';
var_dump(sprintf("[%10s]",    $s)); 
// right-justification with spaces
echo '<br><br>';
var_dump(sprintf("[%-10s]",   $s)); 
// left-justification with spaces
echo '<br><br>';
var_dump(sprintf("[%010s]",   $s)); 
// zero-padding works on strings too
echo '<br><br>';
var_dump(sprintf("[%'#10s]",  $s)); 
// use the custom padding character '#'
echo '<br><br>';
var_dump(sprintf("[%10.10s]", $t)); 
// left-justification but with a cutoff of 10 characters
echo '<br><br><br>';
/* example#7: zero-padded integers */
var_dump(sprintf("%04d-%02d-%02d", 2006, 12, 18));
echo 
'<br><br><br>';
/* example#8: formatting currency */
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
var_dump(sprintf("%01.2f", $money)); 
// output "123.10"
echo '<br><br>';
/* example#9: scientific notation */
$number = 362525200;
var_dump(sprintf("%.3e", $number)); 
// outputs 3.63e+8

?>

  18 EXERCISE   

<?php

setlocale
(LC_NUMERIC, "is_IS", "is-IS", "is_IS.UTF-8");

var_dump(sprintf("%.3f", 100.426));
echo 
'<br><br>';
var_dump(sprintf("%.2f", 100.426));
echo 
'<br><br>';
var_dump(sprintf("%f'",  100.426));
echo 
'<br><br><br>';
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;

var_dump(sprintf("%01.2f", $money));
echo 
'<br><br>';
var_dump(sprintf("%.3e", $money));

?>

  19 EXERCISE   

<?php

/*
* Testing sprintf() : 
* with different unexpected values for 
* format argument other than the strings
*/

echo "Testing sprintf() :<br>
        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
      
0,
      
1,
      
12345,
      -
2345,

      
// float data
      
10.5,
      -
10.5,
      
10.1234567e10,
      
10.7654321E-10,
      
.5,

      
// array data
      
array(),
      array(
0),
      array(
1),
      array(
1, 2),
      array(
'color' => 'red', 'item' => 'pen'),

      
// null data
      
NULL,
      
null,

      
// boolean data
      
true,
      
false,
      
TRUE,
      
FALSE,

      
// empty data
      
"",
      
'',

      
// object data
      
new sample(),

      
// undefined data
      
@$undefined_var,

      
// unset data
      
@$unset_var,

      
// resource data
      
$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 {
    
var_dump(sprintf($value));
  } catch (
TypeError $exception) {
    echo 
$exception->getMessage() . "<br>";
  }

  
// with two arguments
  
try {
    
var_dump(sprintf($value, $arg1));
  } catch (
TypeError $exception) {
    echo 
$exception->getMessage() . "<br>";
  }

  
// with three arguments
  
try {
    
var_dump(sprintf($value, $arg1, $arg2));
  } catch (
TypeError $exception) {
    echo 
$exception->getMessage() . "<br>";
  }

  
$count++;
}

// close the resource
fclose($file_handle);

?>

  20 EXERCISE   

<?php

echo "Testing sprintf() :<br>
               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
      
0,
      
1,
      
12345,
      -
2345,

      
// float data
      
10.5,
      -
10.5,
      
10.1234567e10,
      
10.7654321E-10,
      
.5,

      
// array data
      
array(),
      array(
0),
      array(
1),
      array(
1, 2),
      array(
'color' => 'red', 'item' => 'pen'),

      
// null data
      
NULL,
      
null,

      
// boolean data
      
true,
      
false,
      
TRUE,
      
FALSE,

      
// empty data
      
"",
      
'',

      
// string data
      
"string",
      
'string',

      
// object data
      
new sample(),

      
// undefined data
      
@$undefined_var,

      
// unset data
      
@$unset_var,

      
// resource data
      
$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
  
var_dump( sprintf($format, $value) );

  
// with three arguments
  
var_dump( sprintf($format, $value, $arg2) );

  
$count++;
};

// closing the resource
fclose($file_handle);

?>

  21 EXERCISE   

<?php

setlocale
(LC_ALL, 'de-DE', 'de_DE');

echo 
"Testing sprintf() :<br>
             float formats with integer values.<br>"
;

// array of int type values
$integer_values = array (
  
0,
  
1,
  -
1,
  -
2147483648, 
  
// max negative integer value
  
-2147483647,
  
2147483647,  
  
// max positive integer value
  
2147483640,
  
0x123B,      
  
// integer as hexadecimal
  
0x12ab,
  
0Xfff,
  
0XFA,
  -
0x80000000, 
  
// max negative integer as hexadecimal
  
0x7fffffff,  
  
// max positive integer as hexadecimal
  
0x7FFFFFFF,  
  
// max positive integer as hexadecimal
  
0123,        
  
// integer as octal
  
01, 
  
// should be quivalent to octal 1
  
-020000000000, 
  
// max negative integer as octal
  
017777777777  
  
// max positive integer as octal
);

// various float formats
$float_formats = array(
  
"%f", "%lf", " %f", "%f ",
  
"\t%f", "\n%f", "%4f", "%30f",
);

$count = 1;
foreach(
$integer_values as $int_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$float_formats as $format) {
    
// with two arguments
    
var_dump( sprintf($format, $int_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

?>

  22 EXERCISE   

<?php

echo "Testing sprintf() :<br>
                string formats with string values.<br>"
;

// defining different heredoc strings
/* string created using Heredoc (<<<) */
$heredoc_string = <<<EOT
This is string defined
using heredoc.
EOT;

/* heredoc string with only numerics */
$heredoc_numeric_string = <<<EOT
123456 3993
4849 string
EOT;

/* null heardoc string */
$heredoc_empty_string = <<<EOT
EOT;
$heredoc_null_string = <<<EOT
NULL
EOT;

// array of strings used to test the function
$string_values = [
  
"",
  
" ",
  
'',
  
' ',
  
"string",
  
'string',
  
"NULL",
  
'null',
  
"FALSE",
  
'true',
  
"\x0b",
  
"\0",
  
'\0',
  
'\060',
  
"\070",
  
"0x55F",
  
"055",
  
"@#$#$%%$^^$%^%^$^&",
  
$heredoc_string,
  
$heredoc_numeric_string,
  
$heredoc_empty_string,
  
$heredoc_null_string
];

// array of string formats
$string_formats = [
  
"%s", "%ls", " %s", "%s ",
  
"\t%s", "\n%s", "%4s", "%30s",
];

$count = 1;
foreach(
$string_values as $string_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$string_formats as $format) {
    
var_dump( sprintf($format, $string_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

?>

  23 EXERCISE   

<?php

echo "Testing sprintf() :<br>
               char formats with char values,<br>"
;

// array of char values
$char_values = array( 'a', "a", 67, -67, 99, ' ', '', 'A', "A" );

// array of char formats
$char_formats = array(
  
"%c", "%lc", " %c", "%c ",
  
"\t%c", "\n%c", "%4c", "%30c",
);

$count = 1;
foreach(
$char_values as $char_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$char_formats as $format) {
    
var_dump( sprintf($format, $char_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

?>

  24 EXERCISE   

<?php

echo "Testing sprintf() :<br>
           octal formats with integer values.<br>"
;

// array of integer values
$integer_values = array(
  
0,
  
1,
  -
1,
  -
2147483648, 
  
// max negative integer value
  
-2147483647,
  
2147483647, 
  
// max positive integer value
  
2147483640,
  
0x123B,   
  
// integer as hexadecimal
  
0x12ab,
  
0Xfff,
  
0XFA,
  -
0x80000000, 
  
// max negative integer as hexadecimal
  
0x7fffffff,  // max positive integer as hexadecimal
  
0x7FFFFFFF,  
  
// max positive integer as hexadecimal
  
0123,        
  
// integer as octal
  
01,       
  
// should be quivalent to octal 1
  
-020000000000, 
  
// max negative integer as octal
  
017777777777  
  
// max positive integer as octal
);

// array of octal formats
$octal_formats = array(
  
"%o", "%lo", " %o", "%o ",
  
"\t%o", "\n%o", "%4o", "%30o",
);

$count = 1;
foreach(
$integer_values as $integer_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$octal_formats as $format) {
    
var_dump( sprintf($format, $integer_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

?>

  25 EXERCISE   

<?php
 
echo "Testing sprintf() :<br>
                 integer formats with integer values.<br>"
;

// different valid  integer values
$valid_ints = array(
  
0,
  
1,
  -
1,
  -
2147483648, 
  
// max negative integer value
  
-2147483647,
  
2147483647,  
  
// max positive integer value
  
2147483640,
  
0x123B,      
  
// integer as hexadecimal
  
0x12ab,
  
0Xfff,
  
0XFA,
  -
0x80000000, 
  
// max negative integer as hexadecimal
  
0x7fffffff,  
  
// max positive integer as hexadecimal
  
0x7FFFFFFF,  
  
// max positive integer as hexadecimal
  
0123,        
  
// integer as octal
  
01,       
  
// should be quivalent to octal 1
  
-020000000000, 
  
// max negative integer as octal
  
017777777777  
  
// max positive integer as octal
);
// various integer formats
$int_formats = array(
  
"%d", "%ld", " %d", "%d ",
  
"\t%d", "\n%d", "%4d", "%30d",
);

$count = 1;
foreach(
$valid_ints as $int_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$int_formats as $format) {
    
var_dump( sprintf($format, $int_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

echo 
"Done";
?>

  26 EXERCISE   

<?php
echo "Testing sprintf() :<br>
                 hexa formats with integer values.<br>"
;

// array of integer values
$integer_values = [
  
0,
  
1,
  -
1,
  -
2147483648, 
  
// max negative integer value
  
-2147483647,
  
2147483647,  
  
// max positive integer value
  
2147483640,
  
0x123B,      
  
// integer as hexadecimal
  
0x12ab,
  
0Xfff,
  
0XFA,
  -
0x80000000, 
  
// max negative integer as hexadecimal
  
0x7fffffff,  
  
// max positive integer as hexadecimal
  
0x7FFFFFFF,  
  
// max positive integer as hexadecimal
  
0123,        
  
// integer as octal
  
01,       
  
// should be quivalent to octal 1
  
-020000000000, 
  
// max negative integer as octal
  
017777777777  
  
// max positive integer as octal
];

// array of hexa formats
$hexa_formats = [
  
"%x", "%xx", "%lx", " %x", "%x ",
  
"\t%x", "\n%x", "%4x", "%30x",
];

$count = 1;
foreach(
$integer_values as $integer_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$hexa_formats as $format) {
    
var_dump( sprintf($format, $integer_value) );
    echo 
'<br><br>';
  }
  
$count++;
};


?>

  27 EXERCISE   

<?php

echo "Testing sprintf() :<br>
              unsigned formats with integer values.<br>"
;

// array of integer values
$integer_values = [
  
0,
  
1,
  -
1,
  -
2147483648, 
  
// max negative integer value
  
-2147483647,
  
2147483647,  
  
// max positive integer value
  
+2147483640,
  
0x123B,      
  
// integer as hexadecimal
  
0x12ab,
  
0Xfff,
  
0XFA,
  -
0x80000000, 
  
// max negative integer as hexadecimal
  
0x7fffffff,  
  
// max positive integer as hexadecimal
  
0x7FFFFFFF,  
  
// max positive integer as hexadecimal
  
0123,        
  
// integer as octal
  
01,       
  
// should be quivalent to octal 1
  
-020000000000, 
  
// max negative integer as octal
  
017777777777  
  
// max positive integer as octal
];

// array of unsigned formats
$unsigned_formats = [
  
"%u", "%lu", " %u", "%u ",
  
"\t%u", "\n%u", "%4u", "%30u",
];


$count = 1;
foreach(
$integer_values as $integer_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$unsigned_formats as $format) {
    
var_dump( sprintf($format, $integer_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

?>

  28 EXERCISE   

<?php

echo "Testing sprintf() :<br>
              scientific formats with integer values.<br>"
;

// array of integer values
$integer_values = array(
  
0,
  
1,
  -
1,
  -
2147483648, 
  
// max negative integer value
  
-2147483647,
  
2147483647,  
  
// max positive integer value
  
2147483640,
  
0x123B,      
  
// integer as hexadecimal
  
0x12ab,
  
0Xfff,
  
0XFA,
  -
0x80000000, 
  
// max negative integer as hexadecimal
  
0x7fffffff,  
  
// max positive integer as hexadecimal
  
0x7FFFFFFF,  
  
// max positive integer as hexadecimal
  
0123,        
  
// integer as octal
  
01,       
  
// should be quivalent to octal 1
  
-020000000000, 
  
// max negative integer as octal
  
017777777777  
  
// max positive integer as octal
);

// array of scientific formats
$scientific_formats = array(
  
"%e", "%le", " %e", "%e ",
  
"\t%e", "\n%e", "%4e", "%30e",
);

$count = 1;
foreach(
$integer_values as $integer_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$scientific_formats as $format) {
    
var_dump( sprintf($format, $integer_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

?>

  29 EXERCISE   

<?php

echo "Testing sprintf() :<br>
          scientific formats with float values.<br>"
;
          
// array of float values
$float_values = array(
  -
2147483649,
  
2147483648,
  -
0x80000001, 
  
// float value, beyond max negative int
  
0x800000001, 
  
// float value, beyond max positive int
  
020000000001, 
  
// float value, beyond max positive int
  
-020000000001, 
  
// float value, beyond max negative int
  
0.0,
  -
0.1,
  
1.0,
  
1e5,
  -
1e5,
  -
1e5,
  +
1e5,
  
1e+5,
  -
1e-5,
  
1E8,
  -
1E9,
  
10.0000000000000000005,
  
10.5e+5
);

// array of scientific formats
$scientific_formats = array(
  
"%e", "%le", " %e", "%e ",
  
"\t%e", "\n%e", "%4e", "%30e",
);


$count = 1;
foreach(
$float_values as $float_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$scientific_formats as $format) {
    
var_dump( sprintf($format, $float_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

echo 
"Done";
?>

  30 EXERCISE   

<?php

echo "Testing sprintf() :<BR>
                 with  typical format strings.<BR>"
;

// initialising required variables
$tempnum = 12345;
$tempstring = "abcdefghjklmnpqrstuvwxyz";

echo
"<br><br>Testing for '%%%.2f' as the 
                      format parameter:<br>"
;
var_dump(sprintf("%%%.2f", 1.23456789e10));

echo
"<br><br>Testing for '%%' as the 
                     format parameter:<br>"
;
var_dump(sprintf("%%", 1.23456789e10));

echo
"<br><br>Testing for precision value 
                     more than maximum:<br>"
;
var_dump(sprintf("%.988f", 1.23456789e10));

echo
"<br><br>Testing for invalid 
                     width(-15) specifier:<br>"
;
try {
    
var_dump(sprintf("%030.-15s", $tempstring));
} catch (
ValueError $e) {
    echo 
$e->getMessage(), "\n";
}

echo
"<br><br>Testing for '%X' as the 
                     format parameter:<br>"
;
var_dump(sprintf("%X", 12));

echo
"<br><br>Testing for multiple 
                     format parameters:<br>"
;
var_dump(sprintf("%d  %s  %d\n", $tempnum, 
                                                     
$tempstring, 
                                                     
$tempnum));

echo
"<br><br>Testing for excess of 
                      mixed type arguments :<br>"
;
var_dump(sprintf("%s", $tempstring, 
                                     
$tempstring, 
                                     
$tempstring));

?>

  31 EXERCISE   

<?php

echo "Testing sprintf() :<br>
            with  white spaces in format strings.<br><br>"
;
            
// setlocale(LC_ALL, 'pt_BR', 'pt-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

foreach($formats as $format) {
  
var_dump( sprintf($format, 1234) );
  echo 
'<br><br>';
}

?>

  32 EXERCISE   

<?php

$formats 
= ['s', 'd', 'u', 'f', 'c', 'x'];
$values = [null, false, true, 2, 3.5, "foo", [], [1], 
                  
fopen(__FILE__, "r"), new stdClass];

foreach (
$formats as $format) {
    foreach (
$values as $value) {
        echo 
"$format with " . (is_resource($value) ? 
                
"resource" : md5($value)) . ":<br>";
        try {
            echo 
sprintf("%" . $format, $value), "<br>";
        } catch (
Error $e) {
            echo 
$e->getMessage(), "<br>";
        }
        echo 
"<br>";
    }
}

?>

  33 EXERCISE   

<?php

echo "Testing sprintf() :<br>
                float formats with float values.<br>"
;
                
setlocale(LC_ALL, 'es_AR', 'es-AR');

// array of float type values

$float_values = array (
-
2147483649, 
// float value
  
2147483648,  
  
// float value
  
-0x80000001, 
  
// float value, beyond max negative int
  
0x800000001, 
  
// float value, beyond max positive int
  
020000000001, 
  
// float value, beyond max positive int
  
-020000000001, 
  
// float value, beyond max negative int
  
0.0,
  -
0.1,
  
10.0000000000000000005,
  
10.5e+5,
  
1e5,
  -
1e5,
  
1e-5,
  -
1e-5,
  
1e+5,
  -
1e+5,
  
1E5,
  -
1E5,
  
1E+5,
  -
1E+5,
  
1E-5,
  -
1E-5,
  
.5e+7,
  -
.5e+7,
  
.6e-19,
  -
.6e-19,
  
.05E+44,
  -
.05E+44,
  
.0034E-30,
  -
.0034E-30
);

// various float formats
$float_formats = array(
  
"%f", "%lf", " %f", "%f ",
  
"\t%f", "\n%f", "%4f", "%30f",
);

$count = 1;
foreach(
$float_values as $float_value) {
  echo 
"<br><br>Iteration: $count<br>";

  foreach(
$float_formats as $format) {
    
var_dump( sprintf($format, $float_value) );
    echo 
'<br><br>';
  }
  
$count++;
};

?>

  34 EXERCISE   

<?php
echo "Testing sprintf() :<br>
                   error conditions.<br>"
;

// Zero arguments
echo "<br><br>Testing sprintf() function 
                      with Zero arguments:<br>"
;
try {
    
var_dump( sprintf() );
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";
}

echo 
"<br><br>Testing sprintf() function 
                     with less than expected 
                            number of arguments:<br>"
;
$format1 = '%s';
$format2 = '%s%s';
$format3 = '%s%s%s';
$arg1 = 'one';
$arg2 = 'two';

// with one argument less than expected
try {
    
var_dump( sprintf($format1) );
} catch (
\ArgumentCountError $e) {
    echo 
$e->getMessage(), "<br>";
}
try {
    
var_dump( sprintf($format2,$arg1) );
} catch (
\ArgumentCountError $e) {
    echo 
$e->getMessage(), "<br>";
}
try {
    
var_dump( sprintf($format3,$arg1,$arg2) );
} catch (
\ArgumentCountError $e) {
    echo 
$e->getMessage(), "<br>";
}

// with two argument less than expected
try {
    
var_dump( sprintf($format2) );
} catch (
\ArgumentCountError $e) {
    echo 
$e->getMessage(), "<br>";
}
try {
    
var_dump( sprintf($format3,$arg1) );
} catch (
\ArgumentCountError $e) {
    echo 
$e->getMessage(), "<br>";
}

// with three argument less than expected
try {
    
var_dump( sprintf($format3) );
} catch (
\ArgumentCountError $e) {
    echo 
$e->getMessage(), "<br>";
}

try {
    
var_dump(sprintf('%100$d %d'));
} catch (
\ArgumentCountError $e) {
    echo 
$e->getMessage(), "<br>";
}

try {
    
var_dump(sprintf("foo %", 42));
} catch (
ValueError $e) {
    echo 
$e->getMessage(), "<br>";
}

?>