2. "Backtrace" or report context
A backtrace, or report context (if someone is better as a translation, please ...) you will retrace the path through the call inclusions functions and file to arrive at a given point in the code.
We have a first page (fonctions.php)
Code:
<? php
function b ()
(
var_dump (debug_backtrace ());
)
a function ()
(
b ();
)
a ();
>
Then a second file that call fonctions.php (we call main.php):
Code:
<? php
include 'fonctions.php';
>
Here's what you get:
Code:
array (3) (
[0] =>
array (4) (
[ "file"] =>
string (29) "/ fonctions.php" "
[ "line"] =>
int (10)
[ "function"] =>
string (1) "b"
[ "args"] =>
array (0) (
)
)
[1] =>
array (4) (
[ "file"] =>
string (29) "/ fonctions.php" "
[ "line"] =>
int (13)
[ "function"] =>
string (1) "a"
[ "args"] =>
array (0) (
)
)
[2] =>
array (4) (
[ "file"] =>
string (30) "/ main.php"
[ "line"] =>
int (3)
[ "args"] =>
array (1) (
[0] =>
string (29) "/ fonctions.php"
)
[ "function"] =>
string (7) "include"
)
)
We have three tables, the analysis is done from the last to arrive until the first if we want to trace the path of code from the beginning.
In each chart we have:
function: the function that is called
args: the arguments passed to this function (here, the path to fonctions.php)
line: the row in the code
file: the path to the php file being processed.
In this long thread, to get to the point of tracing (debug_backtrace ()), we note that we change the function include in the main (main.php) followed by a () and by b () fonctions.php in the file.
It may be useful to see how the code behaves in the order of call functions and value of parameters that are given as arguments.
The function debug_backtrace () returns an array containing the report of context from where you've called.
I have displayed directly with var_dump () but you can use as you want, store it in a variable, and so on.
The function debug_print_backtrace (PHP 5) you can directly view the report.
3. How to use crash reports
PHP details of the errors on web pages (or the screen if you use PHP for an application that is not for web).
Feel free to look in detail.
You may have disabled the error report using the @ operator
Use it sparingly, it is useful as it may, for example, you can not give information about your database in an error report, but still manage to think that this error will be displayed not.
Similarly, reporting to PHP you report all errors (default usually), thanks to the error_reporting, you will help you find:
Code:
error_reporting (E_ALL);
4. The global keyword
There is PHP a keyword called "global".
It lets you use a variable of global scope within a function.
Flee this keyword! Do not use it, unless your life is at stake ...
This feature makes the code unreadable, and debugging difficult to follow.
5. The debugger ODA
PHP also has advanced debugging grouped under the name of ODA.
Bookmarks