I'm experimenting with a way to more easily debug Cronjob Scripts with PHP. without having xdebug etc. accessible on the server.
For this, I'd like to get the number of nested structures the code entered. I'll call it NESTING with in the code.
There are at least two methods that use the nesting level in some way that I'm aware of.
break $n; that escapes $n nestings. And goto that doesn't allow entering deeper ones.
<?php
print(__NESTING__);// 1
for($i=0;$i<10;$i++){
print(__NESTING__);// 2
}
print(__NESTING__);// 1
die;
background:
This should work for PHP > 5.3 + 7 so far i tryed googleing the closest i got to the topic is:
ob_get_level()
Code sample:
debugger idea so far:
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;
so one can execute a script, skip debug Lines, skip loops, goback a line, test script behavior with special values, print variables etc..
P.S.: I didn't test the code for errors so it might not run its to explain the idea and what I need the
__NESTING__
for.
Edit1: Expected output:
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)
thankful for any pointers, cheers
PS.: thanks to Rubber duck debugging. noticed that i just need to set the $next_line to after the loop. still if some knows how get so nesting level would be nice to know