Re: PHP newbie: I love it, but...

From: Date: Thu, 08 May 2003 17:38:42 +0000
Subject: Re: PHP newbie: I love it, but...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-146886@lists.php.net to get a copy of this message
Hi Jean, This was annoying to me as well when I first started using PHP. Some bugs are extremely difficult to find, even with a debugger (Zend IDE has one if you'd like to step through). here's a short list of the most common things I find useful in my debugging: -if you get a fatal syntax error and don't see anything wrong, check the previous line to make sure that it ends with a ; -if you get "expected }" make sure your { match up with } -always use $class->var instead of $class->$var unless you intend something like: $var = 'dynamic'; $class->$var = 'my value'; // sets $class->dynamic to 'my value'; $class->var = 'my value'; // sets $class->var to 'my value'; Otherwise, document your code as you are going along, make sure that you understand what you're trying to do before you write a single line of code. In addition, some tricks I use that cut down on errors: If I am writing a block (like an if statement, or a for statement or a function), I always type the full brackets and then fill them in: before: function skeleton() { } after: function skeleton() {
    $firstline = 'this'; // fill in the function definition
} this seriously cuts down on errors. Same is true of quoted string constants and array() statements. For logic errors, you should do comparison testing or unit testing. Make sure your program is divided up into distinct pieces of code that do simple things and combine to do more complicated. If you define the expected inputs and the expected output, it becomes much easier to debug things. var_dump()/print_r() is your friend. So is debug_backtrace() :) Hope this helps you along. Greg -- phpDocumentor http://www.phpdoc.org Jean wrote:
PHP is a very nice language. Been about a week reading O'Reilly & writing my first program. Making some fast progress. But, as I embellish my program, I am beginning to run into trouble. I am finding PHP to be extremely difficult to debug. There's a limit to embedding echo to check the program flow. Then, there are times when the browser shows absolutely nothing. Browser's Show Source Code lists next to nothing; I assume this is because the interpreter hung on a missing brace or something and outputted nothing! Until I become good at spotting my errors, I've got to find a debugging tool. Any words of wisdom would be appreciated. Jean


« previous php.general (#146886) next »