Using printf() to Format a List of Product Prices : Printf « Development « PHP






Using printf() to Format a List of Product Prices

<html>
<head>
<title>Using printf() to format a list of product prices</title>
</head>
<body>
<?php
$products = Array("Pencil"=>"222.4",
                  "Cake"=>"4",
                  "Coffee table"=>80.6
);
print "<pre>";
printf("%-20s%23s\n", "Name", "Price");
printf("%'-43s\n", "");
foreach ( $products as $key=>$val )
    printf( "%-20s%20.2f\n", $key, $val );
printf("</pre>");
?>
 </body>
</html>

           
       








Related examples in the same category

1.Type Specifiers
2.printf: Specifying a Field Width
3.printf: %d
4.printf: %b
5.printf: %X (uppercase X)
6.printf: %x (lowercase x)
7.printf: %s
8.printf: %o
9.printf: %f
10.printf: right-pad with dashes: %'--6s
11.printf: Left-pad the string %s, %-6s
12.printf: octal representation %o
13.printf:hexadecimal representation %X
14.printf: hexadecimal representation %x
15.printf: left-pad a number and specify precision: %05.2f
16.printf: %05f
17.printf: %f, %.2f
18.printf: %d, %c
19.printf %f,%10f,%-010f,%2.2f