While working on one of my PHP
project I got a function debug_backtrace()
at the start of code file as <?php debug_backtrace() || die ("Direct access not permitted"); ?>
<?php debug_backtrace() || die ("Direct access not permitted"); ?>
<?php debug_backtrace() || die ("Direct access not permitted"); ?>
.
While studding on it I got some explanation that it works as:
Debugging PHP issues in a Drupal website can be anything from quick and easy to seriously problematic. PHP includes a debugging function called debug_backtrace , which will print out the chain of code that leads up to the point where the backtrace function is called.
And When I use var_dump()
with debug_backtrace()
I got following result:
array(2) {
[0]=>
array(3) {
["file"]=>
string(61) "C:\xampp\htdocs\folder_name\templates\default\models\home.php"
["line"]=>
int(30)
["function"]=>
string(7) "include"
}
[1]=>
array(4) {
["file"]=>
string(37) "C:\xampp\htdocs\folder_name\index.php"
["line"]=>
int(146)
["args"]=>
array(1) {
[0]=>
string(61) "C:\xampp\htdocs\folder_name\templates\default\models\home.php"
}
["function"]=>
string(7) "include"
}
}
I didn't get that what debug_backtrace()
function exactly work.
Please anyone explain is welcome. Thanks in advance.
Studied links:
debug_backtrace() from registered shutdown function in PHP
Debugging PHP Code with debug_backtrace
Giving a basic example...
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "Start...";
print_r(debug_backtrace());
function t1 () {
echo "Start t1...";
print_r(debug_backtrace());
}
function t2() {
echo "Start t2...";
print_r(debug_backtrace());
t1();
}
echo "before calls...";
print_r(debug_backtrace());
t1();
t2();
Will output...
Start...Array
(
)
before calls...Array
(
)
Start t1...Array
(
[0] => Array
(
[file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
[line] => 22
[function] => t1
[args] => Array
(
)
)
)
Start t2...Array
(
[0] => Array
(
[file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
[line] => 23
[function] => t2
[args] => Array
(
)
)
)
Start t1...Array
(
[0] => Array
(
[file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
[line] => 17
[function] => t1
[args] => Array
(
)
)
[1] => Array
(
[file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
[line] => 23
[function] => t2
[args] => Array
(
)
)
)
I hope this shows that all the debug_backtrace
function does is return what has been called so far. So when you first call t1
, it is simply a stack trace of the call to t1
. The same for the start of t2
, but when t2
calls t1
, the trace lists the call to t2
and t1
.
But also as you can see, that debug_backtrace
from Start..
shows nothing, as there are no levels of code that have caused this trace to be printed.
In your case it calls debug_backtrace
and uses the or die()
to say 'if debug_backtrace
returns nothing, then die()
'
I think the most useful method for debug_backtrace() is to trace which file is the init one start the core, example like
a.php
include 'b.php';
b.php
function test(){
echo('whatever');
}
print_r(debug_backtrace())
php a.php
whateverArray
(
[0] => Array
(
[file] => /opt/php/b.php
[line] => 7
[function] => test
[args] => Array
(
)
)
[1] => Array
(
[file] => /opt/php/a.php
[line] => 3
[args] => Array
(
[0] => /opt/php/b.php
)
[function] => include
)
)
So, you can get from
$backtrace = debug_backtrace()
echo $backtrace[count($backtrace) - 1]['file'];
Get the file who start the calling
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.