10 Tips That Every PHP Developer Should Know, Part 2

I wish I had known these 10 simple things the day I started working
with PHP. This article is part II in the this series and is intended
for newbies. The previous article is located here.

Tip 6: Single and double quotes

Single and double quotes confused me for some time and it really should
not have. I see this quite often in the forum as well. It's very easy
to understand that double quotes allow php to parse and single quotes
do not. Here are some examples:

Tip 7: Problems of style

It's a matter of style and convenience to produce your scripts in such
a way that make them easy to read and debug. If you are using a
programming editor that highlights your code it will be easy to
identify the various parts. This may explain why you find syntax that
looks rather confusing at first. Some examples:

Tip 9: Safe Queries

Safe queries are really a subject for a lengthier tutorial, but I'm
going to try to make a simple presentation here. I'm using functions
in this example as opposed to the more traditional class technique.

A safe query will not return an error message that may reveal path
information or give hackers accidental insider information. Certainly,
security by obscurity is not an effective measure, but reducing error
messages at the user end is desired once your site is launched.

We use the connection class from the previous article and a few
functions to make this happen. Our first function performs the actual
query using msyql_query. If the query string is empty
it will return false.

The next two sample functions are our means for performing queries.
Note that our fetchArray() function will return an
array of results while the fetchRow() function will
simply return a row. If either function returns no results
FALSE will be returned.

With this approach you can also define your queries more specifically
for INSERT, DELETE, etc. and/or for repetitive tasks. Once you have a
group of functions you are comfortable with you can recycle them in
other projects.

If you understand how these safe query functions work then you are
probably ready to explore the commonly used PEAR DB database abstraction
class. This class, which is open source, will give you more
flexibility, debugging features and it will work on more than just a
MySQL database.

A more complete tutorial regarding safe queries can be found at this site. Be sure to read the section regarding
debugging your safe queries as well.

Tip 10: A Strategy for Success

And finally, I highly recommend using a pen, paper and plain english
(or your language of preference) to work out your concepts first.
Chances are that if you can explain what you need to do in plain
language, you will both be able to explain the problem to others and
ultimately solve your problem. You will be surprised how much easier
it will be to program with a plan rather than making it up as you go
along.

Conclusion

For the most part, this collection of 10 things I wish I knew when I
started using PHP are quite simple, but they should be considered
building blocks. Additionally, some of the concepts presented are good
examples of how you can build your own custom functions thus improving
your speed and skill.

Good luck programming!

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.