To debug PHP programs create a file "debug2.inc" having the following functions :
<?php /* define this variable, to prevent double declaration. */ if (!defined("_DEBUG2_DEFINED_")) { define("_DEBUG2_DEFINED_", 1 ); } else return; // if this file is already included then return # file name : debug2.inc # Functions for debuging the PHP source code #***************************************************************** # Copyright policy is GNU/GPL but additional request is # that you include author's name and email on all copies # Author : Al Dev Email: alavoor@yahoo.com #***************************************************************** # Usage of this functions - # In your source code put something like - # debug2_(__FILE__, __LINE__, "f_somevariable", $f_somevariable); # And this will generate output in debug.out file. //function debug2_($fname, $lname, $debug_var, $debug_value=0) {} // Give read, exec for all on directory /debug2_logs // chmod a+rwx /debug2_logs // But here you need to open the file in append mode. $fp_debug2 = fopen("/debug2_logs/debug.out", "a"); if ($fp_debug2 == false) { print "<b>File open failed - global.var.inc<b>"; exit; } function debug2_($fname, $lname, $debug_var, $debug_value=0) { global $fp_debug2; //print "<br> debug_value is : $debug_value <br>"; if (!$debug_value) { fwrite($fp_debug2, "\n ". $fname ." ". $lname .": $debug_var"); } else { fwrite($fp_debug2, "\n ". $fname . " ". $lname .": $debug_var = $debug_value"); } //print "<br> f_cookie is : $f_cookie <br>"; } // In your first page, which is generally index.php3 // truncate the debug2_logs file in beginning of code function init_debug_file() { global $fp_debug2; $fp_debug2 = fopen("/debug2_logs/debug.out", "w"); if ($fp_debug2 == false) { print "<b>File open failed - global.var.inc<b>"; exit; } system("chmod a+rwx /debug2_logs/debug.out"); } ?>
In your PHP source code initial page which is generally index.php3, put a line like
<?php include ("debug2.inc"); init_debug_file(); // all other commands follows here ... // ........... ?>
To output debug values, in your PHP source code files, put debug2_() calls as illustrated below:
<?php include ("debug2.inc"); debug2_(__FILE__, __LINE__, "f_somevariable", $f_somevariable); function aa() { $aa = 8; debug2_(__FILE__, __LINE__, "aa", $aa); } ?>
When you run the PHP program the output will be traced in the file called debug.out giving the filename, linenumber, variable name and it's value.
Use the debug2_() generously in your code. The usage of debug2_() calls in your program will NOT have any impact on the final production code and also has no impact on the performance because they will be filtered out as described below. You can use copy and paste to save time of typing debug2() calls or use the 'yank to buffer' feature of Vi editor and paste.
When you are done development and testing and when you are ready to deploy on the production server, filter out the debug2_ calls from your source code. At unix prompt -
bash$ mkdir production bash$ grep -v debug2_ filea.php3 > production/filea.php3
bash$ mkdir production bash$ ls *.php3 | while read ans do grep -v debug2_ $ans > production/$ans done