PHP/Development/Printf

Материал из Web эксперт
Перейти к: навигация, поиск

printf: %05f

<?php
printf ("left-pad numbers with zeroes: %05f\n", 33.22);
?>



printf: %b

<html>
<head>
<title>printf: %b</title>
</head>
<body>
<?php
$number = 543;
printf("Binary: %b<br>", $number );
?>
</body>
</html>



printf: %d

<html>
<head>
<title>printf: %d</title>
</head>
<body>
<?php
$number = 543;
printf("Decimal: %d<br>", $number );
?>
</body>
</html>



printf: %d, %c

<?php
printf ("The character value of %d is %c\n", 72, 72);
?>



printf: %f

<html>
<head>
<title>printf: %f</title>
</head>
<body>
<?php
$number = 543;
printf("Double: %f<br>", $number );
?>
</body>
</html>



printf %f,%10f,%-010f,%2.2f

 :
<pre>
<?php
$value = 3.14159;
printf("%f,%10f,%-010f,%2.2f\n", $value, $value, $value, $value);
?>
</pre>



printf: %f, %.2f

<?php
printf ("Control the number of decimals in %f with %.2f\n", 5.1234, 5.1234);
?>



printf: hexadecimal representation %x

<?php
printf ("The hexadecimal representation of %d is %x\n", 92, 92);
?>



printf: left-pad a number and specify precision: %05.2f

<?php
printf ("left-pad a number and specify precision: %05.2f\n", 33.22);
?>



printf: Left-pad the string %s, %-6s

<?php
printf ("Left-pad the string %s with dashes: %"-6s\n", "foo", "foo");

?>



printf: %o

<html>
<head>
<title>printf: %o</title>
</head>
<body>
<?php
$number = 543;
printf("Octal: %o<br>", $number );
?>
</body>
</html>



printf: octal representation %o

<?php
printf ("octal representation is %o)\n", 92);

?>



printf: right-pad with dashes: %"--6s

<?php
printf ("right-pad it with dashes: %"--6s\n", "foo", "foo");

?>



printf: %s

<html>
<head>
<title>printf: %s</title>
</head>
<body>
<?php
$number = 543;
printf("String: %s<br>", $number );
?>
</body>
</html>



printf: Specifying a Field Width

<?
print "<pre>";
printf("%20s\n", "Books");
printf("%20s\n", "CDs");
printf("%20s\n", "Games");
printf("%20s\n", "Magazines");
print "</pre>";
?>



printf: %x (lowercase x)

<html>
<head>
<title>printf: %x</title>
</head>
<body>
<?php
$number = 543;
printf("Hex (lower): %x<br>", $number );
?>
</body>
</html>



printf: %X (uppercase X)

<html>
<head>
<title>printf: %X</title>
</head>
<body>
<?php
$number = 543;
printf("Hex (upper): %X<br>", $number );
?>
</body>
</html>



Type Specifiers

/*
Specifier       Description
d               Display argument as a decimal number
b               Display an integer as a binary number
c               Display an integer as ASCII equivalent
f               Display an integer as a floating-point number (double)
o               Display an integer as an octal number (base 8)
s               Display argument as a string
x               Display an integer as a lowercase hexadecimal number (base 16)
X               Display an integer as an uppercase hexadecimal number (base 16)
*/



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>