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

As a final step, you can add the following code to the beginning of

the script to enable all possible error and warning messages. The PHP

scripting engine is very good at locating syntax and run-time errors,

so you should use it whenever possible. Sometimes, the PHP script-

Ing engine can even help locate logic errors.

// DEBUG: Show all warnings

error_reporting(E_ALL | E_STRICT);

When you finish, your code should look like the following:

// DEBUG: Show all warnings

error_reporting(E_ALL | E_STRICT);

$TerritorialCapitals = array(

array("Territory" => "Nunavut",

"Capital" => "Iqaluit"),

array("Territory" =>

"Northwest Territories",

"Capital" => "Yellowknife"),


APPENDIX E

array("Territory" =>

"Yukon Territory",

"capital" => "Whitehorse"));

// DEBUG: Verifying $TerritorialCapitals

echo "<pre>\n";

echo "\$TerritorialCapitals => ";

print_r($TerritorialCapitals);

echo "</pre>\n";

// DEBUG: End verifying $TerritorialCapitals

$TerritoryCount=count($TerritorialCapitals);

// DEBUG: Verifying $TerritoryCount

echo "<p>\$TerritoryCount = " .

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

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

// DEBUG: Verifying $i

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

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

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

" is the capital of " .

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

"</p>\n";

}

668

When you run the script this time, much more information is avail-

able to you. First, examine the XHTML source code, as shown in

Figure E-4. Although the debugging has created extra code, you

should be able to find the lines that display the names of the territo-

ries and capitals. Review those lines to see what data is actually miss-

ing. This is important because Web browsers often do not display data

that is embedded in malformed tags. Without looking at the source

code, you cannot tell if the data is actually missing or just hidden.

Figure E-4

Source code for a PHP script with debugging statements

Now that you’ve confirmed that the data is actually miss-

ing, you can review the debugging information available on the


APPENDIX E

page itself, as shown in Figure E-5. At the top of the page, the

$TerritorialCapitals[] array is populated as expected. The next

message indicates that the $TerritoryCount variable is set to 3.

669

Figure E-5

Web page for a PHP script with debugging statements

APPENDIX E

670

In the next line, you can see that $i is set to 1 and that Yellowknife is

displayed as the capital of the Northwest Territories. The array output

at the beginning of the page shows that the array index for the ele-

ment with "Northwest Territories" and "Yellowknife" is indeed

1. The array index for "Nunavut" and "Iqaluit" is 0. So, to display

Nunavut, you need to adjust the starting value of $i to 0.

Next, you see that $i gets incremented to 2, which is expected.

After that, the PHP scripting engine displays a notice that the index

Capital is undefined. By looking at the array output, you can see that

for $TerritorialCapitals[2], the index of the nested array element

is "capital" with a lowercase ‘c’. Change this to an uppercase ‘C’.

After displaying the line with the missing capital, you see that $i is set

to 3, followed by two warning messages about an undefined offset of

3. Look at the lines identified by the message, and note that these two

lines display the values of the $TerritorialCapitals[] array. Again,

look back to the array output, and note that the last array element is

2, not 3. To fix this, you need to change the comparison in the for

loop from $i<=$TerritoryCount to $i<$TerritoryCount.

After making the three changes to the code and removing all of the

debugging statements, run the script again. You will see the expected

results, as shown in Figure E-6. By using a combination of debugging

techniques, you found and fixed all of the errors much faster and

more easily than if you had applied a single technique.

Figure E-6

Output from a PHP script with the errors fixed

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