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.

554 Chapter 26 Debugging<br />

This warning makes it very easy to correct. Few people would try to write code that<br />

attempted to divide by zero on purpose, but neglecting to check user input often results<br />

in this type of error.<br />

The following code sometimes generates the same error but might be much harder to<br />

isolate <strong>and</strong> correct because it happens only some of the time:<br />

$i = 10;<br />

$k = $i/$_REQUEST['input'];<br />

This is one of many different runtime errors that you might see while testing your code.<br />

Common causes of runtime errors include the following:<br />

n Calls to functions that do not exist<br />

n Reading or writing files<br />

n Interaction with <strong>MySQL</strong> or other databases<br />

n Connections to network services<br />

n Failure to check input data<br />

We briefly discuss each of these causes in the following sections.<br />

Calls to Functions That Do Not Exist<br />

Accidentally calling functions that do not exist is easy.The built-in functions are<br />

often inconsistently named.Why does strip_tags() have an underscore, whereas<br />

stripslashes() does not?<br />

It is also easy to call one of your own functions that does not exist in the current<br />

script but might exist elsewhere. If your code contains a call to a nonexistent function,<br />

such as<br />

nonexistent_function();<br />

or<br />

mispeled_function();<br />

you will see an error message similar to this:<br />

Fatal error: Call to undefined function: nonexistent_function()<br />

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

Similarly, if you call a function that exists but call it with an incorrect number of parameters,<br />

you will receive a warning.<br />

The function strstr() requires two strings: a haystack to search <strong>and</strong> a needle to find.<br />

If instead you call it using<br />

strstr();<br />

you will get the following warning:<br />

Warning: Wrong parameter count for strstr() in<br />

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

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

Saved successfully!

Ooh no, something went wrong!