Cameron Garnett/Testpage11

From WikiEducator
Jump to: navigation, search
PHP

Super Quick PHP Tutorial

Introduction | Basic Syntax | Sending Data to the Browser | Comments | Variables | Strings | Functions | Quotes | Debugging | Form


VmvIcon Objectives.png

Learning Objectives

By the end of this page you will know some technique's used when you are debugging php code.

Debugging

Return values returns an associative array describing the last error with keys "type", "message", "file" and "line". If the error has been caused by a PHP internal function then the "message" begins with its name. Returns NULL if there hasn't been an error yet


VmvIcon Example.png

Example

<?php
echo $a;
print_r(error_get_last());
?>

VmvIcon Example.png

This should output something similar to:

Array
(
[type] => 8
[message] => Undefined variable: a
[file] => C:\WWW\index.php
[line] => 2
)


In the next example the debug_backtrace() function is used. debug_backtrace() generates a PHP backtrace.

VmvIcon Example.png

Example

<?php
// filename: /tmp/a.php

function a_test($str)
{
echo "\nHi: $str";
var_dump(debug_backtrace());br> }

a_test('friend');
?>

<?php
// filename: /tmp/b.php
include_once '/tmp/a.php';
?>

VmvIcon Example.png

This should out put something similar to:

Hi: friend array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}