10 PHP Tips I Wish I Had Known
I have been using PHP for about six years now and have never stopped learning. These are a few things I wish I had known when I began programming in PHP:
- Quotes: Understand the difference between double-quotes and single-quotes. I personally do not like the usage of variables in double quotes:
$var = "my var's value"
I much prefer
$var = 'my var\'s value'
That is a personal preference but it really is extremely important to know when to use different types of quotes and understand escape sequences.
- Don’t get == confused with ===: The triple equals checks if the operands are identical — that is they have the same type with all of the same properties. Example with string:
if('1' == 1){...} // evaluates to true if('1' === 1){...} // evaluates to false - Use print_r liberally: If there is ever confusion when it comes to objects or arrays, print_r will come in extremely handy. Also remember that print_r($var) will print the output as soon as possible (generally at the top of your page) where print_r($var, true) will return it.
- Make a database wrapper class: When you eventually delve into the mySQL (or any other database) world, there will be a ton of new functions to learn: mysql_connect, mysql_select_db, mysql_query, mysql_num_rows, mysql_fetch_array to name just a few. Make it easy by making a single database class to wrap all of this together. It will save you a ton of time and headache in the end.
- Avoid the <? shorthand: Always use the full <?php. This can cause problems with PHP installations that do not have the shorthand enabled and also confuse some XML parsers.
- Never rely on register_globals: Always use $_GET, $_POST, and other pre-defined variables. This guarantees compatibility with other PHP installations.
- Choosing a PHP Version: Use a PHP version you are comfortable with: PHP4 classes are incredibly simple to use. There is no visibility keywords to worry about and functions can be called after being instantiated or statically. PHP5 introduces the vast majority of the OOP paradigm, including visibility, the static keyword, class constants, method overloading, and class abstraction. If you have never been exposed to this, it can be quite difficult to take in while also learning PHP. That being said, it never hurts to use PHP5+ as it is fully backwards compatible.
- Modulize: After working with PHP for a couple of days or so, consider modulizing your commonly used functions, classes, etc. This can be as simple as breaking them into files. I found that I reuse the same code (albeit much cleaned up) that I did 3 years ago.
- Get into OOP as soon as possible: I remember the first year or two I was using PHP, I made maybe six websites for various organizations I was involved with. The code was so horribly sloppy it still makes me sick to this day. Do not get in the habit of mixing your PHP and HTML! I cannot emphasize this enough! Get in the habit of keeping the vast majority of code in included files and simply call function in the HTML itself.
- Utilize the PHP documentation fully: PHP, in my opinion, has bar-none the best documentation of any scripting language. php.net contains a page dedicated to every function in PHP along with examples and alternatives for each. At the bottom there is usually a wealth of user-submitted comments which provide even more information.
2008
8
Jul
- Posted by Aaron Rosenfeld at 12:02 am
- Permalink for this entry
- Filed under: Web Development
- RSS comments feed of this entry
- TrackBack URI
$var . ‘ is my var\’s value’
:o)