Programming Requirements:
- End every statement with semicolon (though, not language
constructs like loops and conditionals)
- Balance quotation marks, parentheses, curly braces, and square brackets
- Single quotation marks must be closed with single quotations
marks, same with double quotation marks
- Escape (\) single- and double-quoatation marks w/in strings
Debugging Techniques:
- Run PHP scripts through URL
- Know version of PHP: PHP_VERSION, or phpinfo()
- Turn on display_errors
- Use error_reporting(E_ALL)
- Check HTML source code
- Use comments
- Use print (echo) statements
- Use single quotation marks to print variables literally
- Use double quotation marks to print variable values
- Use die() and/or exit() language constructs
- Use following functions to print array values:
print_r($myArray), var_export($myArray), var_dump($myArray)
Testing PHP Code (errors.php):
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include("file_with_errors.php");
?>
Common PHP Errors
Blank Page |
HTML issue, or PHP error (and display_errors or error_reporting turned off) |
Parse Error |
Missing semicolon (;), unbalanced curly braces, parentheses, or
quotation marks, or use of an unescaped quotation mark in a string (\") |
Empty variable value |
Missing $, misspelled or incorrect cased variable name, or
inappropriate variable scope (when using functions) |
Undefined variable |
Reference made to variable before it is given a value, or an
empty variable value |
Call to undefined function |
Misspelled function name, PHP not configured to use function
(e.g., MySQL function must be explicitly allowed in configuration
file), or include file containing function definition not included |
Cannot redeclare function |
Two definitions of user-defined function exist, check included files |
Headers already sent |
White space exists in script before PHP tags, data has already
been printed, or a file has been included. |
Debugging Examples:
Example1 (incorrect function name):
$firstName="John";
$nameLength=str_len($firstName);
$myVar="Hi, my first name is " . $nameLength . " letters (actually bytes) long.";
Solution: should be strlen(), not str_len()
Hi, my first name is 4 letters (actually bytes) long.
Example2 (using echo statements):
$myNum=10;
if($myNum > 2)
{
//some code
}
else
{
//some other code
}
Solution: Use echo statements w/in code blocks
$myNum=10;
if($myNum > 2)
{
echo "true path..."
//some code
}
else
{
echo "false path..."
//some other code
}
Example3 (use print_r(), var_export(), or var_dump() functions):
$myArray = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
Solution: use print_r($myArray), var_export($myArray), or var_dump($myArray)
/*
print_r() output is more human-readable, and can store output into a variable, var_dump() cannot.
var_dump displays data types, the others do not.
var_export() is similar to var_dump() with one exception: the returned representation is valid PHP code
NOTE:
var_export cannot handle recursive arrays.
Can't do var_export($_GLOBALS);, as $_GLOBALS contains a reference to itself
*/
print_r($myArray);
var_export($myArray);
var_dump($myArray);
Array
(
[0] => Mon
[1] => Tue
[2] => Wed
[3] => Thu
[4] => Fri
[5] => Sat
[6] => Sun
)
1
array (
0 => 'Mon',
1 => 'Tue',
2 => 'Wed',
3 => 'Thu',
4 => 'Fri',
5 => 'Sat',
6 => 'Sun',
)array(7) {
[0]=>
string(3) "Mon"
[1]=>
string(3) "Tue"
[2]=>
string(3) "Wed"
[3]=>
string(3) "Thu"
[4]=>
string(3) "Fri"
[5]=>
string(3) "Sat"
[6]=>
string(3) "Sun"
}
Example4 (use die() or exit() language constructs):
Stop processing code at some particular juncture.
Solution: use die() or exit() language constructs to stop processing
exit("stop here");
//or...
//die("stop here");
stop here