PHP/Utility Function/printf

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

Adding the
 and 
tags so the spaces display

   <source lang="html4strict">

<?php

printf("
Space padding can be tricky in HTML % 5d.
", 42);

?>

 </source>
   
  


Demonstrating Some Type Specifiers

   <source lang="html4strict">

<html> <head> <title>Demonstrating Some Type Specifiers</title> </head> <body>

<?php

   $number = 543;
   printf("Decimal: %d
", $number ); printf("Binary: %b
", $number ); printf("Double: %f
", $number ); printf("Octal: %o
", $number ); printf("String: %s
", $number); printf("Hex (lower): %x
", $number ); printf("Hex (upper): %X
", $number );

?>

</body> </html>

 </source>
   
  


Displaying a real number in money format

   <source lang="html4strict">

<?php printf("Please pay $%.2f. ", 42.4242); ?>

 </source>
   
  


Displaying long text with an ellipsis

   <source lang="html4strict">

<? printf(substr_replace("this is a test"," ...",5)); ?>

 </source>
   
  


Displaying signs with printf()

   <source lang="html4strict">

<? $min = -40; $max = 40; printf("The computer can operate between %+d and %+d degrees Celsius.", $min, $max); ?>

 </source>
   
  


Displaying the same number in different formats

   <source lang="html4strict">

<?php $value=42; printf("%d
",$value); printf("%b
",$value); printf("%c
",$value); printf("%f
",$value); printf("%o
",$value); printf("%s
",$value); printf("%x
",$value); printf("%X
",$value); ?>

 </source>
   
  


Example: Displaying a number in binary format

   <source lang="html4strict">

<?php

   printf("The computer stores the number 42 internally as %b.",42);

?>

 </source>
   
  


Format an integer and a floating-point value with the printf() function.

   <source lang="html4strict">

<?php $i = 123456; $f = 98765.567; printf("\$i = %x and \$i = %b\n", $i, $i); printf("\$i = %d and \$f = %f\n", $i, $f); printf("\$i = %09d and \$f = %0.2f\n", $i, $f); ?>

 </source>
   
  


Format strings for use in printf( )

   <source lang="html4strict">

Format Meaning

%% A literal percent character; no matching parameter is required

%b Parameter is an integer; express it as binary

%c Parameter is an integer; express it as a character with that ASCII value

%d Parameter is a positive integer; express it as decimal

%f Parameter is a float; express it as a float

%o Parameter is an integer; express it as octal

%s Parameter is a string; express it as a string

%x Parameter is an integer; express it as hexadecimal with lowercase letters

%X Parameter is an integer; express it as hexadecimal with uppercase letters <?

   $number = 123;
   printf("123 in binary is: %b", $number);
   printf("123 in hex is: %h", $number);
   printf("123 as a string is: %s", $number);
   printf("%% allows you to print percent characters");
   $number = 123.456;
   $formatted = number_format($number, 2) . "\n";
   print "Formatted number is $formatted\n";
   printf("Formatted number is %.2f\n", $number);

?>

 </source>
   
  


Formatting a price with printf()

   <source lang="html4strict">

<? $price = 5; $tax = 0.075; printf("The dish costs $%.2f", $price * (1 + $tax)); ?>

 </source>
   
  


int printf ( string format [, mixed argument [, mixed ...]] ) is the standard C way to format text

   <source lang="html4strict">

<?

   $animals = "lions, tigers, and bears";
   printf("There were %s - oh my!", $animals);

?>

 </source>
   
  


Left-aligned by prepending a minus symbol (-) to the field width specifier

   <source lang="html4strict">

<?

   printf ("%-20s\n", "Left aligned");

?>

 </source>
   
  


Padding Output with the Padding Specifier

   <source lang="html4strict">

<?

   printf("%04d", 36);

?>

 </source>
   
  


Pad with leading zeroes:

   <source lang="html4strict">

<?

   $red = 1;
   $green = 1;
   $blue = 1;
   printf( "#%02x%02x%02x", $red, $green, $blue);

?>

 </source>
   
  


printf and format

   <source lang="html4strict">

<?php printf("$%01.2f", 43.2); printf("%d beer %s", 100, "bottles"); printf("%15s", "Some text"); ?>

 </source>
   
  


printf() and sprintf() Formatting Types

   <source lang="html4strict">

Type Description % A literal percent character. No argument is required. b The argument is treated as an integer and presented as a binary number. c The argument is treated as an integer and presented as the character with that American Standard Code for Information Interchange (ASCII) value. d The argument is treated as an integer and presented as a (signed) decimal number. e The argument is treated as scientific notation (for example, 1.2e+2). u The argument is treated as an integer and presented as an unsigned decimal number. f The argument is treated as a float and presented as a floating-point number (locale aware). F The argument is treated as a float and presented as a floating-point number (nonlocale aware). Available since PHP 4.3.10 and PHP 5.0.3. o The argument is treated as an integer and presented as an octal number. s The argument is treated and presented as a string. x The argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). X The argument is treated as an integer and presented as a hexadecimal number (with upper-case letters).

 </source>
   
  


printf and string format

   <source lang="html4strict">

<?php printf("The %2\$s likes to %1\$s", "bark", "dog"); // The dog likes to bark printf("The %1\$s says: %2\$s, %2\$s.", "dog", "bark"); // The dog says: bark, bar. ?>

 </source>
   
  


printf() and Type Specifiers

   <source lang="html4strict">

Specifier Description

d Displays an argument as a decimal number

b Displays an integer as a binary number

c Displays an integer as its ASCII equivalent

f Displays an integer as a floating-point number (double)

o Displays an integer as an octal number (base 8)

s Displays an argument as a string

x Display an integer as a lowercase hexadecimal number (base 16)

X Displays an integer as an uppercase hexadecimal number (base 16)

 </source>
   
  


@printf("

Maths: %d; English: %d; History: %d; Biology: %d.

\n"

   <source lang="html4strict">

<?php

 $scores = array(88, 75);
 @list($maths, $english, $history) = $scores;
@printf("

Maths: %d; English: %d; History: %d; Biology: %d.

\n",$maths, $english, $history, $biology);

?>

 </source>
   
  


printf puts the numbers into the string

   <source lang="html4strict">

<?php printf("The computer stores the numbers 42, and 256 internally as %b and %b.",42,256); ?>

 </source>
   
  


printf() Type Specifiers

   <source lang="html4strict">

Option Value

% A literal percent characters (takes no parameters)

b Integer represented as a binary number (for example: 101110111)

c Integer represented as the character corresponding to its ASCII value

d Integer represented as a signed integer number

u Integer represented as an unsigned number

f Floating-point value

o Integer represented as an octal value

s String value

x Integer value represented in hexadecimal notation (with lowercase characters)

X Integer value represented in hexadecimal notation (with uppercase characters)

 </source>
   
  


The printf function formats complex strings using a single expression.

   <source lang="html4strict">

<?php

   $n = 15.32;
   $log = log ($n); 
   printf ("log (%0.2f) = %.5f\n", $n, $log); 

?>

 </source>
   
  


Use printf to output float numbers

   <source lang="html4strict">

$hamburger = 4.91235; $milkshake = 1.12395; $cola = .85; $food = 123.123; $tax = 1.075; $tip = 1.16; printf("%1d %9s at \$%.2f each: \$%.2f\n", 2, "Hamburger", $hamburger, 2 * $hamburger); printf("%1d %9s at \$%.2f each: \$%.2f\n", 1, "Milkshake", $milkshake, $milkshake); printf("%1d %9s at \$%.2f each: \$%.2f\n", 1, "Cola", $cola, $cola); printf("%25s: \$%.2f\n", "Food and Drink Total", $food); printf("%25s: \$%.2f\n", "Total with Tax", $food + $tax); printf("%25s: \$%.2f\n", "Total with Tax and Tip", $food + $tax + $tip);

 </source>
   
  


Using printf() to Format a List of Product Prices

   <source lang="html4strict">

<html> <head> <title>Using printf() to Format a List of Product Prices</title> </head> <body> <?php $products = array (

     "A"=>2.4,
     "B"=>4,
     "C"=>80.6
     );
print "
";
printf ("%-20s%23s\n", "Name", "Price");
printf ("%"-43s\n", "");
foreach ( $products as $Key=>$val ) {
  printf ("%-20s%20.2f\n", $Key, $val);
}
print "
";

?> </body> </html>

 </source>
   
  


When you specify a color in HTML, you combine three hexadecimal numbers between 00 and FF.

   <source lang="html4strict">

<?

   $red = 204;
   $green = 204;
   $blue = 204;
   printf( "#%x%x%x", $red, $green, $blue);

?>

 </source>
   
  


Working with printf()

   <source lang="html4strict">

<? printf ("This is my number: %d", 55); ?>

 </source>
   
  


Zero-padding with printf()

   <source lang="html4strict">

<? $zip = "6520"; $month = 2; $day = 6; $year = 2007; printf("ZIP is %05d and the date is %02d/%02d/%d", $zip, $month, $day, $year); ?>

 </source>