Practical PHP

Preview:

DESCRIPTION

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) - PowerPoint PPT Presentation

Citation preview

Practical PHP

Using printf

• 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).

The printf conversion specifiers

• 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

• 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);

Precision Setting

• 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);• ?>

Precission Setting

• 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

String Padding

• 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); // Left justify, pad '@', cutoff 6 chars

Padding Value

• [House]• [ House]• [House ]• [00000House]• [#####House]• [ Doctor H]• [Doctor ]• [Doctor@@@@]

String conversion

Using sprintf

• $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;

Date and Time Functions

• 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

Date and Time Functions

Date and Time Functions

Date Constants

File Handling

• 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

Creating a File

• 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";• ?>

XHTML Versions

• 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

Recommended