debugging my php codes
i just learned about php, so i was very eager to try it out. the first problem was how to test php offline, which i solved and blogged it on my previous post. here's a link in case you missed it. test php offline
now a new problem arises.
i figured out how to test it offline, so i quickly wrote some php codes. first i was, "wow, i'm doing it!" then i have to do my assignment in that class. if you are wondering why i have an assignment, it's because i'm taking this course in a free lms site. it's called village88.com . here's a link of a post about it about village88.com.
as i was saying, i'm doing my assignment. the assignment was to create a page that somehow does what a facebook wall does. the user can create an account, have a wall where they can post some status and other users can comment/reply on a post. they can also delete their post but only their own post or comment/reply. the assignment sounds cool, right! so i hurriedly created the pages. i coded the html, added some css to make it look good. created the forms for log-in and registration purposes. i layed out the pages for the wall and profile. everything was looking good. then i inserted the php codes. then everything started to head south.
when i ran the pages, wow, my browser displayed rows and rows of error messages. from mysql errors to missing variable errors to unexpected etc etc. i was down. i really thought this was going to be easy. though feeling down, i pursued, debugged the codes one by one. for hours, i continued tracking down all that nasty bugs until they are all gone.
in the end, i learned my lessons which i am going to share to you all so you won't have to experience what have i been through. though this is not 100% bullet proof, at least it will lessen the hours spend debugging errors.
-
semi-colon
this is the nastiest culprit of all times. you can spend hours and hours finding that bug and in the end you'll find out that it is a single semi-colon you forgot to place in the end of the line. a piece of advice, always have that semi-colon ready whenever you enter a new line. type that semi-colon, press left arrow once, then, go with your code. take some time to repeatedly do it when coding and your muscle will memorize the steps. before you know it, it is already embedded in your coding style. -
terminators ")}] ?>"
like semi-colon, terminators can be hard to track down specially when you have a hundreds of lines of codes. best way to deal with this is to readily place your terminators before proceeding with your codes. just like how you deal with semi-colons. just a reminder, if you don't readily place terminators it will easily be very tricky where to place them afterwards. -
functions
always use functions. use functions to connect to mysql. use functions to redirect to a page. use functions to prevent mysql injections. use functions to display database query results. this way, your code will look cleaner and compact. the lesser the codes the easier to detect the bugs. -
mysql
sometimes it's the connection, some other times it's the query. sometimes you just misspelled the table name or field name. either ways, it's harder to debug if you don't really know your mysql. master it, and then proceed with php.
that's all folks. hope you learned something from me. 'til next time!