So when you say, "Why isn't this thing working?" the answer is almost always, "Because you told it not to."
Working incrementally toward an objective is a good idea. By this, I mean I've found that taking small steps is a good strategy. Instead of building the entire program and seeing if it works, make the building blocks work as you're creating your program.
Visual Basic may not be quite so easy. You may need to build up the foundation before you can test your program.
Here are some tips I hope will be helpful.
- Use break points (in Visual Studio). At certain points within your code, you'll expect things to happen. You may expect an array to be populated, a variable to be filled or a counter to be incremented. Using a break point to inspect the contents just may give you the clue you need.
- Use var_dump(). In PHP, var_dump() is not to be underestimated. Knowing the state of objects and the contents of variables is never more than an echo statement away. For example, instead of using echo "$variableOne"; you should use echo var_dump($variableOne);. If you had instead used an echo statement, you will receive no output if the contents of $variableOne are null. Using var_dump(), you will either see that the variable's contents are null, or a description of the variable itself and also the contents.
- Finish everything that you start. When creating tag pairs in HTML or XML, create the pair and then fill in the text and attributes. This should keep you from later inspecting each element from line 1 down, looking for an elusive closing tag.
- Use a good text editor. You can write code in any language with notepad. However, it won't help you with indents. Or color coding. Or tag matching. Using a text editor like Komodo-Edit (http://www.activestate.com/komodo-edit/downloads), NotePad++ (http://notepad-plus-plus.org/download) or JEdit (http://www.jedit.org/index.php?page=download) will save you time and errors. These are not the only full-featured text editors, but they should get you started.
Rather than using echo statements to debug in PHP, try var_dump. Echo statements are often used in code legitimately and therefore it can be a pain to have to search for all of them to get rid of your debugging echo statement. Also, var_dump display a NULL in such a case whereas echo will display nothing.
ReplyDeleteThanks, Kris. Great point.
ReplyDeleteUsed var_dump quite a bit recently, works much better than echo.
ReplyDeleteWish I'd used it last semester.
I have used var_dump() and echo quite a bit. I've found that both are useful when I add text before them. So instead of just var_dump($variable) I'll use something like
ReplyDeleteecho "The contents of '$variable' on line xx are " . var_dump($variable);
or
echo "The contents of '$variable' on line xx are " . $variable;
$