6.Debugging PHP 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&
gt;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;
PHP HOW−TO
6.Debugging PHP 9
$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
For a large group of files −
bash$ mkdir production
bash$ ls *.php3 | while read ans
do
grep −v debug2_ $ans > production/$ans
PHP HOW−TO
6.Debugging PHP 10
done
And now copy the files from production to the deployment area.
7.Limitations of PHP Everything has limitations or disadvantages and PHP is no exception. The following are the limitations of
PHP (so be
WARNED !!)
1. PHP is NOT 100 % pure Object Oriented scripting language. PHP is good if your PHP code size does
not exceed 3,00,000 lines. Maintainence of PHP code greater than 1,00,000 lines becomes more
difficult.
2. PHP will NOT give the performance of "C" or "C++" language. Because it is slanguage and
is interpreted it will be a bit slower than the optimized "C++" programs. For top performance, you
should use "C++" and fast−CGI with database/webserver connection pooling and use C++ compiler
optimizer "−O3" options. Zend optimizer in PHP 4 will speed up the performance of PHP to certain
extent.
On the other hand, PHP has lot of advantages and it''s advantages outweigh it''s limitations −
1. You can very rapidly develop web applications in PHP as compile and link is eliminated in PHP
scripting language.
2. PHP applications are very stable and do not depend on the browser technologies unlike Javascript
applications which depend on browsers. PHP will give you the freedom to select any server platform
and browser does not know that the HTML page is generated by PHP!!
3. PHP has excellent database conectivity to all SQL database servers.
4. PHP has partial support for Object oriented features
5. PHP has C++, Perl, Javascript like syntax features and has programs like ''ptags/ctags'' to navigate the
source code
6. PHP has Zend optimizer which speeds up the performance
7. PHP runs on all unixes, linux, Windows 95/NT/2000 and is more powerful than ASP, JSP and others.
8. PHP has a very large user base and developer base.
WARNING: If you want 100% pure Object Oriented scripting language than you MUST consider
Python.
The ''Python'' is a object oriented scripting language from ground up. You would be using the Python Web
Application server called ''Zope'' which is available at −
http://www.zope.org and python is at
8.Related URLs Visit following locators which are related to C, C++ −
&
middot; Vim color text editor for C++, C http://metalab.unc.
edu/LDP/HOWTO/Vim−HOWTO.html
· SQL database server for PHP PostgreSQL
http://metalab.unc.edu/LDP/HOWTO/PostgreSQL−HOWTO.html
· Source code control system CVS HOWTO for C++ programs
PHP HOW−TO
7.Limitations of PHP 11
http://metalab.unc.edu/LDP/HOWTO/CVS−HOWTO.html
· Linux goodies main site http://www.aldev.8m.com
· Linux goodies mirror site http://aldev.webjump.com
9.Other Formats of this Document This document is published in 11 different formats namely − DVI, Postscript, Latex, Adobe Acrobat PDF,
LyX, GNU−info, HTML, RTF(Rich Text Format), Plain−text, Unix man pages and SGML.
· You can get this HOWTO document as a single file tar ball in HTML, DVI, Postscript or SGML
formats from − ftp://metalab.unc.edu/pub/Linux/docs/HOWTO/other−formats/ or
ftp://metalab.unc.edu/pub/Linux/docs/HOWTO/other−formats/
· Plain text format is in: ftp://metalab.unc.edu/pub/Linux/docs/HOWTO or
ftp://metalab.unc.edu/pub/Linux/docs/HOWTO
· Translations to other languages like French, German, Spanish, Chinese, Japanese are in
ftp://metalab.unc.edu/pub/Linux/docs/HOWTO or
ftp://metalab.unc.edu/pub/Linux/docs/HOWTO Any help from you to translate to other languages is
welcome.
The document is written using a tool called "SGML tool" which can be got from −
http://www.xs4all.nl/~cg/sgmltools/ Compiling the source you will get the following commands like
· sgml2html PHP−HOWTO.sgml (to generate html file)
· sgml2rtf PHP−HOWTO.sgml (to generate RTF file)
· sgml2latex PHP−HOWTO.sgml (to generate latex file)
This document is located at −
· http://metalab.unc.edu/LDP/HOWTO/PHP−HOWTO.html
Also you can find this document at the following mirrors sites −
· http://www.caldera.com/LDP/HOWTO/PHP−HOWTO.html
· http://www.WGS.com/LDP/HOWTO/PHP−HOWTO.html
· http://www.cc.gatech.edu/linux/LDP/HOWTO/PHP−HOWTO.html
· http://www.redhat.com/linux−info/ldp/HOWTO/PHP−HOWTO.html
· Other mirror sites near you (network−address−wise) can be found at
http://metalab.unc.edu/LDP/hmirrors.html select a site and go to directory
/LDP/HOWTO/PHP−HOWTO.html
In order to view the document in dvi format, use the xdvi program.
More reviews about the Debugging php