我正在尝试一种更容易地用PHP调试Cronjob脚本的方法。 无需在服务器上访问 xDebug 等。
为此,我想获取代码输入的嵌套结构的数量。 我将在代码中称之为NESTING。
据我所知,至少有两种方法以某种方式使用嵌套级别。
打破$n;逃脱$n嵌套。并且转到不允许进入更深的。
<?php
print(__NESTING__);// 1
for($i=0;$i<10;$i++){
print(__NESTING__);// 2
}
print(__NESTING__);// 1
die;
背景:
这应该适用于 PHP> 5.3 + 7 到目前为止,我尝试在谷歌上搜索最接近该主题的是:
ob_get_level()
代码示例:
到目前为止的调试器想法:
argv[1]='somefile.php;'
<?php
$script=fopen(argv[1],'rb');
$tfilename='T'.substr(md5(mt_rand()), 0, 7).argv[1];//some random name
$tscript=fopen($tfilename,'wb+');
fputs($tscript,fgets($script).' $next_line=0;$nest_limit=1;'); //<?php +ini
$line_num=0;
while($line=fgets($script)){
$line='L'.($line_num++).': '.
'if((__LINE__>$next_line)&&(__NESTING__<=$nest_limit)){'.
'print("Line".__LINE__.date('r'));eval(rtrim(fgets(STDIN)));'.
'}'.$line;
fputs($tscript,$line);
}
fclose($tscript);
fclose($script);
print('debug script?');
while(rtrim(fgets(STDIN))==='y'){
try{include $tfilename;}catch(Exeption $e){print($e->getMessage());}
print('repeat?');
}
unlink($tfilename);
die;
所以可以执行脚本, 跳过调试行, 跳跃循环, 回一条线, 使用特殊值测试脚本行为, 打印变量等。
PS:我没有测试代码的错误,所以它可能不会运行它来解释这个想法和我需要什么
__NESTING__
为。
编辑1: 预期产出:
Line0 10:00:20 \ press enter
Line1 10:00:20 \ input: $next_line=10;
Line10 10:00:20 \ enter
Line11 10:01:10 \ someting takes a lot of time
PHP FATAL ERROR...
2cond try
Line0 10:01:15 \ input $next_line=11
Line11 10:02:20 \ input var_dump($something)
感谢任何指示, 干杯
PS.:感谢橡皮鸭调试。 注意到我只需要将 $next_line 设置为 After 循环。 仍然如果有些人知道如何获得如此嵌套级别会很高兴知道
如果我理解你的问题,听起来你正在尝试合并一个计数器,看看你的脚本在死亡/中断/跳过之前运行了多少次。 这应该是相当直接的
$count = 0;
while ($line = fget($script)) {
$count++;
if ($foo = $bar) {
$fooBar = $line;
break;
}
if ($baz == 2) {
goto 45;
}
//other random logic
}
echo "The script executed " . $count . " times.";
如果你想在逻辑之后计数,或者只计算它进入代码特定部分的次数,你可以设置其他变量来计算细节,或者将计数器移动到你想要看到它计数的确切位置。
但是,如果您正在迭代一个固定数字,如您的第一个示例所示
for ($i = 0, $i < 10, $i++)
那么这有点没用,因为您已经知道正在执行的次数。 唯一的例外是,如果你的循环中有一个中断,并且你试图在中断之前计算它执行的次数,那么你仍然可以使用计数器来获取该信息。