Jump to content


Popular Content

Showing content with the highest reputation on 09/12/2017 in all areas

  1. 1 point
    4. "Advanced" debugging techniques for any PHP code (In reality, these things aren't "advanced" as in "difficult to grasp", but they do require some extra setup, hence they're not really "basic" either) 4.1. Use an editor with "syntax highlighting" for PHPJust because PHP can be written with just Notepad doesn't mean that you have to use it. In fact, it's a bad idea to do so, unless of course you have no other choice. There are countless other editors, and if you just search "PHP editor", you'll get a large list of editors, all of which have a feature known as "syntax highlighting". What this means is that the editor will color different parts of your code differently, based on what PHP is expected to interpret that part as. It might sound like just a silly cosmetic, but that's in fact the quickest way of finding syntax/parse errors in your code. For example, if you know that your editor colors strings as one color and loops with another, yet see a loop with the color of a string, you already know that you have a string above the loop that you haven't closed. Vice versa? You probably forgot to close another string above the mis-colored string. No colors? Chances are you're not within a "<?php" block. Unconvinced? This forum has a syntax highlighting feature built in if you use the "code" BBCode tag. It tries to guess the language, and color accordingly. An explicit "<?php" is enough to make it turn on PHP. Next time you have a syntax/parse error, "Preview" your post, where the code is surrounded in a "code" BBCode tag, and see if you can spot your error. While we're on the subject of editors... 4.2. Prefer editors with "code completion" for PHP What "code completion" (sometimes called "auto complete", "Intellisense" or other kinds of marketing nonsense) means is the ability of an editor to suggest possible things you may be trying to type. In addition to saving you time in typing new code, this ability is also useful for debugging - if you can't see a variable/function/class/whatever in the list of suggestions, chances are that you've mistyped it and/or that the editor is smart enough to know that you can't use that thing at that point. There aren't many editors with "code completion" for PHP, due to the fact that PHP is loosely typed and interpreted, which means that more often than not, anything could potentially be everything everywhere (simply put, things become known when you run the file, not while you're typing it), which means that editors need to be a lot smarter, and allow themselves the potential of giving you wrong information (and we don't want that, do we?). If you keep your code organized and in small isolated compositions (functions, classes, methods, etc.), use type hinting everywhere it makes sense, and use "DocBlocks" to document all of your code, you probably won't encounter misleading editor hints. Some editors with "code completion" for PHP include Adobe Dreamwaver CS6 (earlier versions only support it for built-in PHP functions and nothing more, while this version also includes support for PHP classes and your own functions/classes), NetBeans (7.2 also supports traits), PhpStorm, Eclipse PDT and Aptana Studio. 4.3. Install XDebugXDebug is the de facto standard PHP debugger, and for several good reasons too. The more you get used to it, the more its absence on other people's machines will frustrate you . The most "basic" thing that XDebug is worth getting for is that it makes the output for var_dump() colorful (using HTML with CSS styles...), which may sound like just a cosmetic fluff, but if you're var_dump()-ing large arrays or objects, you'll find the output is significantly easier to read. Another, more important feature, is function stack traces. What this means is that if an error occurs, you can see the exact sequence of functions that was called to produce the error, starting with one in the file that execution started at. Combined with a var_dump() of the input data (starting with the last called function, and going back one by one to the first), you can quickly find the point at which your code started to behave weirdly (and thus resulting in the error further on). There are other "fancy" features in it too, such as code coverage, line-by-line debugging and profiling, but for best experience, you need additional tools (e.g. NetBeans , PhpStorm, Eclipse PDT and Aptana Studio support line-by-line debugging after you configure them for it; KCachegrind and Webgrind can view profiling information, etc.).