Jump to content

PHP/Debugging

From Wikiversity
< PHP
(Redirected from PHP Debugging)

PHP Course > PHP Debugging

Introduction

[edit | edit source]

Debugging PHP can become an arduous task at times. However, with the implementation of certain techniques, the time with which it takes to debug can be drastically reduced. This page will try to elaborate on the most useful of those techniques.

Compilation Debugging

[edit | edit source]

More content is needed here.

Runtime Debugging

[edit | edit source]

When dealing with runtime errors, the following steps should be followed:

Debugging Steps

[edit | edit source]
  1. Determine the most probable point of error (this may take some guess and check)
  2. Print out the variable with the highest likelihood of error, or print all variables
  3. Stall the rest of the script execution
  4. Assess the variables contents
  5. Tweak the script and using guess and check, determine if the error has been removed

Essentially the goal is to see if your code is producing the expected result, and if not, tweaking the code until you have removed the error.

Example

[edit | edit source]

Notice the second line contains a variable name that does not exist and is therefore null, so although we expect we will get . By printing it out, we can realize that the error must occur on the second line, because that is only place in which is present. In doing so, we see that the variable $outpt, is just the incorrect spelling of $output, and we have found our error.

<?php

$output=5*4;
$output=$outpt+2; // where the error occurs

print_r($output); // print the variable to understand content
die(); // stall the script to isolate the problem

?>

Debugging Functions

[edit | edit source]

Function: print_r

[edit | edit source]

This function allows you to print the contents of any variable, including a constructed object and an array. As discussed above, this can be used in part (2). PHP Manual: print_r

<?php
$variable="Hello World";
print_r($variable);

$array=array('a', 'c', 'd', 'b');
print_r($array);
?>

Returns

Hello World

Array ( [0] => a [1] => c [2] => d [3] => b )

You can also utilize this to print out all of the variables that are defined (with global scope) at any point in the script. Using the statement:

<?php
print_r($GLOBALS);
?>

The $GLOBALS variables is discussed below.

This function is very similar to print_r, but it also gives more information on the type of variable as well as the defined value. PHP Manual: var_dump

<?php
$variable="Hello World";
var_dump($variable);

$array=array('a', 'c', 'd', 'b');
var_dump($array);
?>

Returns

string(23) "Hello World"

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "c"
  [2]=>
  string(1) "d"
  [3]=>
  string(1) "b"
}

Function: die() and exit()

[edit | edit source]

The die() and exit() functions are equivalent. Both functions allows you to halt the execution of further code. This is very useful when debugging because it allows you to isolate the problem, and understand where the error is occurring.

Example

<?php

echo "Hello";
die(); // or exit(), it's preference or exit("Text to output") or die("Text to output");
echo " World";

?>

This will only return "Hello", because the script will have "died" after the call to the die() function.

Variables

[edit | edit source]

$GLOBALS

[edit | edit source]

The $GLOBALS variable is an associative array containing all variables which are currently defined in the global scope.[1] An understanding of variable scope may be required in understanding how $GLOBALS will not include variables that do not have global scope. However, for most intensive purposes, using $GLOBALS to find problems with code can be extremely useful.

See also

[edit | edit source]