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

APPENDIX E

rather than the Web browser’s formatted display. Often you can see

problems in the source code that you cannot see in the browser win-

dow itself, such as empty <p> tags, missing attributes or values, and

fields with incorrect values. Most Web browsers allow you to view the

underlying XHTML code for a Web page.

For example, assume you were attempting to use the

$_SERVER['SCRIPT_NAME'] autoglobal as the value of the action

attribute of a <form> tag, but typed $_SERVER['SCIPT_NAME']

Instead. By looking at the source code, you could see that the value of

the action attribute of the <form> tag was empty. This would indicate

the source of the error.

Combining Debugging Techniques

As you can see from the preceding examples, no single technique will

find all errors. It is often more efficient to track down a bug by com-

bining debugging techniques. For example, consider the following

code, which should display the Canadian territories and capitals:

$TerritorialCapitals = array(

array("Territory" => "Nunavut",

"Capital" => "Iqaluit"),

array("Territory" =>

"Northwest Territories",

"Capital" => "Yellowknife"),

array("Territory" =>

"Yukon Territory",

"capital" => "Whitehorse"));

$TerritoryCount=count($TerritorialCapitals);

for ($i=1;$i<=$TerritoryCount;++$i) {

echo "<p>" . $TerritorialCapitals[$i]["Capital"] .

" is the capital of " .

$TerritorialCapitals[$i]["Territory"] .

"</p>";

}

To view the

source code

for a Web

page in

Mozilla

Firefox 3, you select View

from the menu bar and

then select Page Source.

In Apple Safari 4, you

select View from the

menu bar and then select

View Source. In Microsoft

Internet Explorer 7, you

click the Page button and

then select View Source

from the pop-up menu.

Most Windows browsers

also allow you to view the

source code by right-

clicking the page and

selecting View Source

from the pop-up menu.

In Chapter 3,

you learned

how to use

the \n escape

sequence to

insert a line break at the

end of the string. This

approach is useful when

you look at the script

output in your Web

browser. Without using

\n escape sequences to

format the XHTML source

code, the code will

appear on a single line,

which makes it difficult to

follow.

665

As Figure E-2 shows, this code has some problems. The first territory,

Nunavut, is not displayed anywhere on the list. Whitehorse is not

displayed as the capital of the Yukon Territory, and one line displays

neither a capital nor a territory. Several techniques can help to isolate

the cause of these problems.


APPENDIX E

666

Figure E-2

Web page for a PHP script that is not working correctly

First, examine the code. It is already properly formatted, so you can

tell that the structure of the code blocks and the array declaration are

correct.

Next, examine the script output shown in Figure E-3. As you can see,

the entire body of the Web page source code is on a single line, which

makes it difficult to read through the XHTML code. To help with

debugging, place a \n after the closing </p> tag.

Figure E-3

Unformatted output of a PHP script that is not working correctly

Next, you can use echo statements to verify that variables are

being set correctly. Add the following code immediately after the

$TerritorialCapitals[] array declaration to verify the contents.

The print_r() function will display the contents of the array, and

using the <pre> tag will ensure that the array is displayed correctly on

the Web page.

APPENDIX E

// DEBUG: Verifying $TerritorialCapitals

echo "<pre>\n";

echo "\$TerritorialCapitals => ";

print_r($TerritorialCapitals);

echo "</pre>\n";

// DEBUG: End verifying $TerritorialCapitals

There are three lines of output in Figure E-2, so the $TerritoryCount

variable is probably set correctly. However, you can add the following

line of code after the declaration of $TerritoryCount to verify the

value:

// DEBUG: Verifying $TerritoryCount

echo "<p>\$TerritoryCount = " .

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

667

As with $TerritoryCount, it appears that the for loop is work-

ing correctly because there are three lines of output in Figure E-2.

However, you can add the following line of code to the beginning of

the for loop code block to verify the value of $i:

// DEBUG: Verifying $i

echo "<p>In for loop: \$i = " .

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

Notice that all of the code added for debugging purposes is com-

mented, and that each comment has the text “DEBUG:” before the

description, which makes it easier to find and remove all of the

debugging code once the script is working correctly. Also notice that

the debugging code for the $TerritorialCapitals[] array consists

of multiple statements. As a result, a comment marks both the begin-

ning and end of the debugging code, which helps ensure that all of the

debugging code is removed.

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