As mentioned in a post I recently read http://www.phpbuilder.com/board/show...eadid=10240313 (not all of it, it seams some people can't stop talking here so fast it's like IRC now) some people have NO IDEA whatsoever about debugging and actually finding errors so here are my useless hints :
1. When you code a MySQL query use new lines :
PHP Code:
$sql = "SELECT
`yourtable`,`field1` as f1,
`yourtable`,`field2` as f2
FROM
`yourtable`
";
Why? If you have an error MySQL will give you line number referencing a line INSIDE the query. Always saves time to actually be able to use the info.
2. When you code a MySQL query echo it in a HTML comment (not in production) :
PHP Code:
$sql = "SELECT
`yourtable`,`field1` as f1,
`yourtable`,`field2` as f2
FROM
`yourtable`
";
echo "<!--\n$sql\n-->";
Why? Well if your pages doesn't work try running your querries in phpMyAdmin or your DB manager and see the output. It sometimes is interesting.
More soon (tired now)
Last edited by Bunkermaster; 05-23-2003 at 03:34 AM.
"Our ASP will blot out the sun!"
"Then we will PHP in the shade"
add your tricks here, it is NOT c losed thread (or is it? /me checks) ok it is NOT a closed thread. Just use same coloring as i did so peeps can look at it fast
[COLOR=#FF8000] for tricks titles
Code goes inside [PHP] blocks
"Our ASP will blot out the sun!"
"Then we will PHP in the shade"
if (isset($_COOKIE["me"])) {
Do all the code in here
if ($_COOKIE["me"] == 'poser') {
Do all the code in here
} else {
Do all the code here
} //Poser if
} else {
Do all the code here
} //Is Cookie Set
Why? Because as applications get more and more nested this will help you solve the unexpected $end on line number XXX error and will help you figure out where the close } is.
Last edited by Bunkermaster; 05-23-2003 at 02:20 PM.
It's probably been mentioned before elsewhere, but when constructing strings, sql queries, whatever,....I prefer to escape out of the string (and use single-quotes where possible):
PHP Code:
$moo = 'This is what ' . $cow . 'said to me.';
Instead of:
PHP Code:
$moo = "This is what $cow said to me."
WHen using an editor that syntax-highlights, it makes it MUCH easier to pick out the variables (especially in large or complex strings).
Remove all headers when you are about to debug. Specifically if you are working on an image script. If you output a PNG (or another format) using header("Content-type: Image/PNG"), then no errors can be echoed. Errors can only be seen if something is text/html, not an image. This just results in a broken image, since PHP still echoes out the error.
Edit: Uhh.. I don't know how to use the board's code in the subject, so I took out the [color] thing.
indent your code
this helps to detect missing braces, and can help to spot logical errors, too.
use an editor with syntax highlighting
makes it easy to find unclosed strings or unescaped quotes within string.
[bunkermaster, I think this might be more often needed in the newbie or general forum, maybe other moderators would be willing to place a reference there?]
If your conditional statement isn't working properly, and it's testing for equivalence, check that you've got two equals signs.
I'm just going to expand on this and share a tip that would help if you've been using literals and constants on the right side of an 'if' expression. Example code:
PHP Code:
$value = 2;
if ($value = 1)
echo 'one'; // This gets displayed
elseif ($value = 2)
echo 'two';
The equal comparison operator in PHP requires two equal signs (==) and not just one (=). One equal sign is an assignment operator. In the example code, the '1' was assigned to the $value variable at the first 'if' expression, and this made the expression evaluate to TRUE; causing the echo 'one' to be executed and displayed. With this type of code example, PHP produces no errors (when there's a variable name to the left of the equal sign). This makes debugging this kind of problem more difficult and potentially time consuming. It's not always very apparent.
Here's a tip to avoid this problem in the future. Get in the habit of coding the 'if' expression with the literal value or constant first or to the left of the equal sign, like this:
PHP Code:
$value = 2;
if (1 == $value)
echo 'one';
elseif (2 == $value)
echo 'two'; // This gets displayed
That way if you happen to make a mistake and put just one equal sign again, then PHP will give you a parse error. The code of '1 = $value' is invalid because PHP can't assign the contents of $value to '1' (a literal; it must be a variable). This could potentially save you lots of time in testing/debugging because you get an error alert.
Originally Posted by barand
Check variable contents
Use something like this to check you received what you expected to receive
As the programmer, it is your responsibility to understand what your code is doing and what it is supposed to do; the computer is not capable of doing that for you. Taking chunks of other people's code and inserting them into yours without bothering to work out how to use them is a sure way of turning your code into an unworkable mess.
If it's a class, study its interface to see what it provides and then decide if you can use it to do what you're wanting. If it's just a fragment of code of the sort that is posted here to illustrate a solution, remember that it is just an illustration and not necessarily the real thing. Who knows what it will do when you run it? If you don't, you're in trouble.
Last edited by Weedpacket; 03-03-2007 at 08:19 PM.
Originally posted by Tekron-X The Reason for this is that PHP is not always accurate on its error line numbers.
I wouldn't say that; I'd say that PHP reports the number of the line it was processing when it discovered that there was an error (if you miss out a }, PHP can't tell you where it should have been). So , depending on the error message, even if the error itself is not on the line indicated, that line may provide clues as to where the error actually is. For example, if it says that there was a type conversion error (where, say, an array was used where it expected a string), then one of the variables in that line is at fault: work backwards and see where they get their values from.
Last edited by Weedpacket; 06-14-2003 at 07:26 AM.
Bookmarks