Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

APPENDIX B

of the menu. The WampServer application closes and the icon is

removed from the system tray.

Working with the WampServer Menu

After you install the WampServer, you can launch the application as

you would any other Windows application. Click the Start button on

the taskbar and select Programs | WampServer | Start WampServer.

The WampServer icon appears in the system tray on the taskbar.

With the application running, you can click the WampServer icon to

display available menu options. If you click Apache or MySQL under the

Powered by Anaska heading, you can verify that the service is running.

If you select Apache from the WampServer menu and select Service

from the submenu, you will see that the Stop Service option is active,

but the Start/Resume Service option is grayed out (inactive). You can

use the Stop Service option to stop the Web server while leaving other

services available. Once the service is stopped, the Stop Service option is

grayed out (inactive), but the Start/Resume Service option is active. You

can then use the Start/Resume Service option to restart the Web server.

When you finish using the WampServer, you can click the

WampServer icon and select Stop All Services or right-click the icon

and select Exit.

639

The same

Stop Service

and Start/

Resume

Service

options are available for

all of the WampServer

services.

It is a good

idea to exit

the

WampServer

when you are

not developing Web

pages because it uses

computer resources and

memory.

Accessing WampServer Online

Typically, the local installation of WampServer is for development

purposes and the files are uploaded to a production server after they

have been tested. You probably do not want Web pages saved on your

local server to be globally accessible on the Web. For this reason, the

default option for accessibility is set to Put Offline.

To allow files saved in the root Web folder of your local computer to

be accessed outside your LAN:

1.

2.

You take

security

risks by

allowing

Internet

users to access your

local Web, PHP, and

MySQL servers. Use this

option with caution.

On your LAN gateway, enable port 80 for http in firewalls.

On your LAN gateway, forward port 80 for http to the

Internet Protocol (IP) address of the computer that has

WAMP installed.

From the WampServer menu, click Put Online.

Access your Web files by substituting “localhost” with your

Web IP address, which you can find at the Where is My IP

Web site (whereismyip.com) or by checking your gateway’s

configuration.

3.

4.


APPENDIX B

Installing the Directory Structure

for Student Files

As discussed in the preface, all of the files created and used for the

exercises, Reinforcement Exercises, and Discovery Projects in this

book conform to a standard directory layout. A .zip archive file that

contains the layout and all the files needed to complete the exercises

is available at the Cengage Web site (www.cengage.com). To install the

directories and files, simply unzip the archive file in the Web folder

on your local server (C:\wamp\www\ is the default for WAMP).

640

APPENDIX

Formatting Strings

C

Using the printf() and sprintf()

Functions

PHP includes the printf() and sprintf() functions, which format

text strings for output. The printf() function outputs a text string

directly, similar to the print and echo statements, whereas the

sprintf() function formats a string and returns the formatted value

so that you can assign it to a variable.

Both functions accept as a first argument a format control string,

which contains instructions for formatting text strings. You surround

the format control string with single or double quotation marks, the

same as you do for other types of strings. Each function also accepts

additional arguments containing the data to be formatted by the

format control string. Within the format control string, you include

a conversion specification for each of the data elements you want

to format, along with any other text that you want to appear in the

formatted string. A conversion specification begins with a percent

symbol (%) and specifies the formatting you want to apply to a data

element. You must include a conversion specification for each argu-

ment that is passed to the printf() or sprintf() function after the

format control string. For example, the following code contains a

printf() function with two conversion specifications in the format

APPENDIX C

control string: one for the $FirstName variable and one for the

$SecondName variable:

$FirstName = "Gosselin";

$SecondName = "Gauselin";

printf("<p>The name %s is also spelled %s.</p>\n",

$FirstName, $SecondName);

642

Notice that each conversion specification consists of a percent sym-

bol followed by the letter s. The letter following the percent symbol

in a conversion specification is a type specifier, which determines the

display format of each data argument that is passed to the printf()

or sprintf() function. A type specifier of s simply displays the data

argument as a standard string. The preceding example is equivalent to

the following code, which uses an echo statement.

$FirstName = "Gosselin";

$SecondName = "Gauselin";

echo "<p>The name $FirstName is also spelled $SecondName

</p>\n";

If you are only using a simple %s conversion specification, it will be

easier to use the standard PHP syntax instead. For more complex

formatting, the printf() and sprintf() functions provide options

that are not available in the standard PHP syntax. These options are

explained next.

Specifying Types

Table C-1 lists the type specifiers you can use with the printf() and

sprintf() functions.

Type

Specifier

b

c

d

u

f

o

s

x

X

Description

Displays the argument as a binary integer

Displays the ASCII character for the index specified by the argument

Displays the argument as a decimal integer

Displays the argument as an unsigned decimal integer

Displays the argument as a floating-point number

Displays the argument as an octal integer

Displays the argument as a string

Displays the argument as a lowercase hexadecimal integer

Displays the argument as an uppercase hexadecimal integer

PHP type specifiers

Table C-1


APPENDIX C

The following code demonstrates how to use each of the type speci-

fiers listed in Table C-1. Figure C-1 shows the output.

$Value = 163;

print("<p>\n ");

printf("Binary integer: %b<br />\n ", $Value);

printf("ASCII character: %c<br />\n ", $Value);

printf("Decimal integer: %d<br />\n ", $Value);

printf("Unsigned decimal integer: %u<br />\n ", $Value);

printf("Floating-point number: %f<br />\n ", $Value);

printf("Octal integer: %o<br />\n ", $Value);

printf("String: %s<br />\n ", $Value);

printf("Lowercase hexadecimal integer: %x<br />\n ", $Value);

printf("Uppercase hexadecimal integer: %X<br />\n ", $Value);

print("</p>\n");

643

Figure C-1

Using the

printf() and

sprintf() types

Determining Decimal Number Precision

A common use of the string formatting functions is to format num-

bers to be displayed with a specified number of decimal places. For

example, it’s often necessary to format numbers as currency, with two

decimal places. However, a variable that contains the currency value

you want to display might be an integer that does not contain decimal

places, or it might be a floating-point number that has more than

two decimal places. By default, the f type specifier formats numbers

with six decimal places. To specify a different number of decimal

places, add a period and an integer representing the desired number

of decimal places between the percent symbol and the f type speci-

fier in a conversion specification. In the following code, a value of

99.5 is assigned to the $RetailPrice variable. When the value in the

$RetailPrice variable is multiplied by 5% to add sales tax, the result-

ing value is 104.475, which is assigned to the $PriceWithTax variable.

The value is then formatted to two decimal places with the printf()

statement. Figure C-2 shows the output.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.


APPENDIX C

$RetailPrice = 99.5;

$PriceWithTax = $RetailPrice * 1.05;

printf("<p>The retail price is $%.2f.</p>\n",

$RetailPrice);

printf("<p>The price with 5%% sales tax is $%.2f.</p>\n",

$PriceWithTax);

644

You can only

use a period

and number

of decimals

in a conver-

sion specification that

uses the f type specifier.

Figure C-2 Using the printf() statement

to format a number to two decimal places

Specifying Padding

In addition to specifying the number of decimal places that appear

to the right of a decimal point, you can specify the number of char-

acters used to output the data argument. For example, you might

have a variable that counts the number of visitors to your Web site.

Instead of just displaying the number of visitors, you might want to

format it to display the number of visitors out of a million by pad-

ding the beginning of the number with zeroes. To pad the beginning

of a string with zeroes, include a 0 and an integer representing the

number of characters the number should have between the percent

symbol and type specifier in a conversion specification. For example,

the conversion specification in the following sprintf() statement

specifies that the number should consist of seven characters. Because

the string representation of the $Visitors variable only contains four

characters, the beginning of the number is padded with three extra

zeroes, as shown in Figure C-3.

$DisplayValue = sprintf("%07d", 5767);

echo "<p>You are visitor number " .

$DisplayValue . ".</p>\n";

Use two per-

cent signs

(%%) to include

a percent

symbol as a

character in a format

control string.

Figure C-3

Padding a value with leading zeroes


APPENDIX C

You can also specify that a string should be padded with spaces

instead of a 0. Simply use a space instead of the 0 or exclude the 0 in a

conversion specification. However, most Web browsers automatically

replace multiple spaces on a Web page with a single space, unless you

use the <pre> element. If you want to pad a number with any charac-

ter other than a 0 or a space, you must precede it with a single quo-

tation mark ('). For example, the following code pads a string with

asterisks (*) instead of spaces or zeroes. Figure C-4 shows the output.

$Payment = 1410.23;

printf("<p>Pay the amount of \$%’*9.2f.</p>\n", $Payment);

Padding takes

into account

the number of

characters in

a string, not

the number of digits in an

integer. This includes the

decimal point and deci-

mal places. For example,

the number 345.10 con-

sists of six characters. If

you pad the formatting

string with zeroes and

specify that the format-

ting string should contain

eight characters, the

number will be formatted

as 00345.10.

If you add a

plus sign (+)

immediately

following the

percent sym-

bol in a conversion speci-

fication, positive numbers

are formatted with a plus

sign before them and

negative numbers are

formatted with a minus

sign (-) before them.

Otherwise, only negative

numbers will have a sign

preceding the value.

645

Figure C-4

Padding with asterisks

Formatting Numbers

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]