PHP Troubleshooting
While some of these coding mistakes are syntax errors and easily identifiable by PHP Lint or Error Reporting, some are logical errors. Logical may not trigger any error or warning, but will still produce an undesirable result. They can also be difficult to find, especially for beginners. Just like with error reporting, the best way to find a logic error is to output information to the HTML page as the code is running.
Output Information
The following is a list of functions that allow us to output information to the page, which can help troubleshoot our PHP code.
echo
The echo
function is a way of displaying strings, numbers and variables to the page.
<?php
$string = "I am a string";
echo $string; // I am a string
?>
However, if the variable's value is an array, the echo statement will not return the values in the array. Instead will either display the string "Array", which is not all that helpful, or cause a PHP error, which is worse.
<?php
$array = ["I am an array", "I have many values"];
echo $array // Array
?>
print_r
The print_r()
function displays human-readable information about a variable, and while it can be use on with strings and number, it more commonly used to display arrays.
<?php
$array = ["I am an array", "I have many values"];
print_r($array) // Array ([0] => I am an array [1] => I have many values)
?>
By using the print_r()
function we can see all the value being stored in the array. When combined with the HTML <pre>
tags, the output of the print_r()
function when be neatly formatted when displayed.
<pre>
<?php
$array = ["I am an array", "I have many values"];
print_r($array) // Array ([0] => I am an array [1] => I have many values)
?>
</pre>
var_dump
The var_dump()
function take print_r to the next level, by providing even more information about variables include they current type (string, number, array, etc) and the size of the variable (number characters in the string, number items in the array). The var_dump()
function also display arrays in a nice, human readable format.
<?php
$array = ["I am an array", "I have many values"];
var_dump($array) // array(2) {[0] => string(13) "I am an array" [1] => string(18) "I have many values"}
?>
gettype
The gettype()
function return the variable data type, which can be helpful if we are expecting a number instead of a string.
<?php
$number = 3;
$string = '3';
echo gettype($number) // integer
echo gettype($string) // string
?>
get_defined_vars
The get_defined_vars()
function will return an array of all defined variables, even the variable that are pre-defined by PHP.
<?php
$number = 3;
$string = '3';
$array = ["I am an array", "I have many values"];
var_dump(get_defined_vars());
?>
debug_backtrace
The debug_backtrace()
function will return an array with a list of function calls that led up to the debug_backtrace()
function.
<?php
function say_hello_to($word) {
echo "Hello {$word}<br>";
var_dump(debug_backtrace());
}
say_hello_to('Everyone');
?>
Graceful Handling of Fatal Errors
Sometimes it is necessary to let the end user know an error has occurred, without displaying the error reporting messaging. This can be done using the register_shutdown_function()
and error_get_last()
functions. Together, these functions can be used to create and display a custom error message, as shown below.
NOTE
This technique will not work if there is a syntax error on the page.
<?php
register_shutdown_function(function() {
$error = error_get_last();
if(!empty($error)) {
echo "Huston, we have a problem.";
}
}
?>