1 / 18

Practical PHP

Practical PHP. printf ("There are %d items in your basket", 3) ; If you replace the %d with %b, the value 3 would be displayed in binary (11). Using printf. Table 7-1. The printf conversion specifiers Specifier Conversion action on argument arg Example (for an arg of 123)

tanith
Download Presentation

Practical PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practical PHP

  2. printf("There are %d items in your basket", 3); • If you replace the %d with %b, the value 3 would be displayed in binary (11). Using printf

  3. Table 7-1. The printf conversion specifiers • Specifier Conversion action on argument arg Example (for an arg of 123) • % Display a % character (no arg is required) % • b Display arg as a binary integer 1111011 • c Display ASCII character for the arg { • d Display arg as a signed decimal integer 123 • e Display arg using scientific notation 1.23000e+2 • f Display arg as floating point 123.000000 • o Display arg as an octal integer 173 • s Display arg as a string 123 • u Display arg as an unsigned decimal 123 • x Display arg in lowercase hexadecimal 7b • X Display arg in uppercase hexadecimal 7B The printf conversion specifiers

  4. You can have as many specifiers as you like in a printf function, as long as you pass a matching number of arguments, and as long as each specifier is prefaced by a % symbol. Therefore the following code is valid, and will output “My name is Simon. I’m 33 years old, which is 21 in hexadecimal”: • printf("My name is %s. I'm %d years old, which is %X in hexadecimal”, 'Simon', 33, 33); The printf conversion specifiers

  5. Example 7-1. Precision setting • <?php • echo "<pre>"; // Enables viewing of the spaces • // Pad to 15 spaces • printf("The result is $%15f\n", 123.42 / 12); • // Pad to 15 spaces, fill with zeros • printf("The result is $%015f\n", 123.42 / 12); • // Pad to 15 spaces, 2 decimal places precision • printf("The result is $%15.2f\n", 123.42 / 12); • // Pad to 15 spaces, 2 decimal places precision, fill with zeros • printf("The result is $%015.2f\n", 123.42 / 12); • // Pad to 15 spaces, 2 decimal places precision, fill with # symbol • printf("The result is $%'#15.2f\n", 123.42 / 12); • ?> Precision Setting

  6. The output from this example looks like this: • The result is $ 10.285000 • The result is $00000010.285000 • The result is $ 10.29 • The result is $000000000010.29 • The result is $##########10.29 Precission Setting

  7. Example 7-2. String padding • <?php • echo "<pre>"; // Enables viewing of the spaces • $h = 'House'; • printf("[%s]\n", $h); // Standard string output • printf("[%10s]\n", $h); // Right justify with spaces • printf("[%-10s]\n", $h); // Left justify with spaces • printf("[%010s]\n", $h); // Zero padding • printf("[%'#10s]\n\n", $h); // Use the custom padding character '#' • $d = 'Doctor House'; • printf("[%10.8s]\n", $d); // Right justify, cutoff of 8 characters • printf("[%-10.6s]\n", $d); // Left justify, cutoff of 6 characters • printf("[%-'@10.6s]\n", $d); // Leftjustify, pad '@', cutoff 6 chars String Padding

  8. [House] • [ House] • [House ] • [00000House] • [#####House] • [ Doctor H] • [Doctor ] • [Doctor@@@@] Padding Value

  9. String conversion

  10. $hexstring: $hexstring = sprintf("%X%X%X", 65, 127, 245); • Or you may wish to store output ready to display later on: $out = sprintf("The result is: $%.2f", 123.42 / 12); echo $out; Using sprintf

  11. echo time(); • echo time() + 7 * 24 * 60 * 60; • echo mktime(0, 0, 0, 1, 1, 2000); • The number of the hour (0–23) • The number of the minute (0–59) • The number of seconds (0–59) • The number of the month (1–12) • The number of the day (1–31) • The year (1970–2038, or 1901–2038 with PHP 5.1.0+ on 32-bit signed systems) Date and Time Functions

  12. Date and Time Functions

  13. Date and Time Functions

  14. Date and Time Functions

  15. Date Constants

  16. MySQL is not the only (or necessarily the best) way to store all data on a web server. Sometimes it can be quicker and more convenient to directly access files on the hard disk File Handling

  17. Example 7-4. Creating a simple text file • <?php // testfile.php • $fh = fopen("testfile.txt", 'w') or die("Failed to create file"); • $text = <<<_END • Line 1 • Line 2 • Line 3 • _END; • fwrite($fh, $text) or die("Could not write to file"); • fclose($fh); • echo "File 'testfile.txt' written successfully"; • ?> Creating a File

  18. XHTML documents can be quickly processed by any program that can handle XML files. As more and more devices such as iPhones and BlackBerries become web-enabled XHTML Versions

More Related