Want to write for WebD&D? We are looking for writers. Interested? Drop us an e-mail at hi (at) webdosanddonts.com.

Fundamental PHP coding Do’s & Don’ts

Written by Siniša on May 2, 2010.

There are tons of great PHP articles out there. Most of them cover performance tips and general PHP how-to’s. That’s why I’m going to take a different approach here and try to give you some PHP coding guidelines, many of which are just general good programming practices taken from the PHP perspective.

Commenting like this may take you some extra time, but it is worth it.

Don’t overuse global variables

… or better yet — try to keep them to the bare minimum if not avoiding them altogether :)

This is true for most (if not all) programming languages. Here’s what Wikipedia has to say on the subject:

They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, (unless they reside in protected memory) and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity.

In PHP, global variables are used a little differently than in other languages. They must be declared global inside the function if they are going to be used in that function. I guess it’s a kind of safety mechanism protecting you from unintentionally changing a global variable. But on the other hand if you omit the global declaration by mistake you could end up spending hours on debugging, wondering why the output is not what (you think) it should be. Sounds familliar?

If you want a “real world” example take a look at the archive.php template from WordPress default theme.

Avoid using PHP short tag

Although there can be times when doing something like <?=$foo?> can seem like a convenient thing to do, you should resist the temptation and use <?php echo $foo; ?>.

There are several reasons why you should avoid using the PHP short tag, but here are the most important ones:

On some shared hosts short tags are turned off so in case of moving your site you might end up replacing all of your PHP opening tags

Avoid the confusion with the XML declaration

Short tags will not be supported in future PHP versions

Initialize your variables

Yes — PHP is loosely typed language and no — you don’t have to initalize your variables, but… Initializing variables (and sticking with it’s type from then on) is simply a good coding practice. This will make your code more easy to follow and less error-prone. Especially in larger functions/methods.

There is also a performace benefit since incrementing a initialized variable is much quicker then an uninitialized one.

About the author

Siniša Kolundžić is a wannabe rock drummer and web enthusiast from Croatia. Making his daily bread as a business application developer he always finds the time to contribute to WebD&D.

Sorry, but if you’re using a proper editor with bracket highlighting, after a while the alternative syntax for control structs gets total HORRIBLE and you get lost in them …

.. so better stick to proper curly brackets, which can be easily resolved using above mentioned bracket highlighting (and if you got a proper editor, it’s also able to do some code folding or letting you hop to the begin / end of the bracket ;)).

It’s a matter of preference. Personally, I like using the method in the article because it reads better. In retrospect, when the code get’s complex, brackets, braces, etc don’t hardly read at all. I don’t like having to click every single closing bracket, to see which opening bracket highlights.

There is nothing wrong with making your code more verbose and easier to read.

@Giovani, @fwolf: I hear you guys, but I’m not saying ditch the closing brackets completly… Just consider the alternative when you have to nest a lot of control structures inside HTML template which has a lot of switching between PHP and HTML.

Anyway, thanks for the comments. It’s always good to hear other opinion.

@ryan Singleton == global? No, a well-constructed singleton can be self-contained in its own class using a static class variable. A few of the big reasons against GLOBALS are they can be easily misplaced (What the hell did I name it? Where did I set it up?) or overwritten. With a singleton, you don’t have to worry about it since it’s attached to a class and unless you’re really sloppy with it, it can’t be overwritten. I just wish some of the people I work with would figure that out.

When coding open source for the public, you should code to the least-common-denominator (and disregard the poor servers still running PHP4), which for one thing means no short-tags (since they may be disabled on a server) and not requiring obscure extensions.

@ryan Singleton == global? No, a well-constructed singleton can be self-contained in its own class using a static class variable. A few of the big reasons against GLOBALS are they can be easily misplaced (What the hell did I name it? Where did I set it up?) or overwritten. With a singleton, you don’t have to worry about it since it’s attached to a class and unless you’re really sloppy with it, it can’t be overwritten. I just wish some of the people I work with would figure that out.
+1

Please remove the alternative syntax from this best practices page. First of all it does break bracket balance and it honestly doesn’t look any better if you format your code properly it will look fine and read just as well. My vote is to deprecate the alternative syntax so I don’t have to open a 100k line application that uses only alternative syntax because some web site said it was the right thing to do. At least mention a warning that this breaks balance braces and can potentially be a headache for someone opening your code in the future.