gpt4 book ai didi

php - 获取调用该函数的行?

转载 作者:行者123 更新时间:2023-12-04 15:25:13 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





function name($param, $line = __LINE__, $file = __FILE__) {};

(4 个回答)


8 年前关闭。




我查了一下,我知道答案可能涉及使用 debug_backtrace() ,但我正在努力研究如何使用它或它的确切作用。

基本上,如果这是 index.php:

<?php
//some code
//some more code

require "functions.php";

print_line();

//some code

print_line();
?>

和functions.php是:
<?php
function print_line(){
$line="[line that this function was called at]";
print "This function was called from line $line of index.php<br />";
}
?>

设置 $line 的正确方法是什么?这样输出将是:
This function was called from line 7 of index.php
This function was called from line 11 of index.php

最佳答案

debug_backtrace() 包含从 0(最近的)到当前作用域索引的所有嵌套函数调用。

所以当你需要行 where 打印行()被调用,只需使用:

<?php
function print_line(){
$backtrace = debug_backtrace();

$line=$backtrace[0]['line'];
print "This function was called from line $line of index.php<br />";
}

或者从 PHP 5.4 开始:
$line=debug_backtrace()[0]['line'];

关于php - 获取调用该函数的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18037211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com