13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Programming Errors<br />

553<br />

run into this problem with single <strong>and</strong> double quotation marks <strong>and</strong> also with the various<br />

forms of brackets <strong>and</strong> parentheses.<br />

The following script generates a similar syntax error:<br />

<br />

These errors can be hard to find if they result from a combination of multiple files.They<br />

can also be difficult to find if they occur in a large file. Seeing parse error on<br />

line 1001 of a 1000-line file can be enough to spoil your day, but it should provide a<br />

subtle hint that you should try to write more modular code.<br />

In general, though, syntax errors are the easiest type of error to find. If you make a<br />

syntax error <strong>and</strong> try to execute that block of code, <strong>PHP</strong> will give you a message telling<br />

you where to find your mistake.<br />

Runtime Errors<br />

Runtime errors can be harder to detect <strong>and</strong> fix. A script either contains a syntax error, or<br />

it does not. If the script contains a syntax error, the parser will detect it when that code<br />

is executed. Runtime errors are not caused solely by the contents of your script.They<br />

can rely on interactions between your scripts <strong>and</strong> other events or conditions.<br />

The statement<br />

require ('filename.php');<br />

is a perfectly valid <strong>PHP</strong> statement. It contains no syntax errors.<br />

This statement might, however, generate a runtime error. If you execute this statement<br />

<strong>and</strong> filename.php does not exist or the user who the script runs as is denied read<br />

permission, you will get an error resembling this one:<br />

Fatal error: main() [function.require]: Failed opening required 'filename.php'<br />

(include_path='.:/usr/local/lib/php') in<br />

/home/book/public_html/phpmysql4e/chapter26/error.php on line 1<br />

Although nothing is wrong with the code here, because it relies on a file that might or<br />

might not exist at different times when the code is run, it can generate a runtime error.<br />

The following three statements are all valid <strong>PHP</strong>. Unfortunately, in combination, they<br />

attempt to do the impossible—divide by zero:<br />

$i = 10;<br />

$j = 0;<br />

$k = $i/$j;<br />

This code snippet generates the following warning:<br />

Warning: Division by zero in<br />

/home/book/public_html/phpmysql4e/chapter26/div0.php on line 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!