How To Write Better Php Code: These 7 Ways


How To Write Better Php Code: These 7 Ways

You don’t need to remember functions, classes and specific solutions to be a good programmer. They are online, accessible at any time. And being a PHP developer you are unlikely to do much important work without being connected to the internet. Then what makes a good programmer? For me a good PHP programmer means someone who is efficient – who can solve problems and build quality web based software quickly and in a way that allows easier maintenance and extending of the program.

So being a good PHP programmer means mostly to write a good code with less effort and time spent. And here is what does that mean – several main things:

– Code that is short. I don’t mean putting everything on one line like some Perl coders do, but writing less waste, reusing code and keeping it modular
– Code that is easy to maintain and extend – this again means modular, but also a well commented PHP code.
– Code that doesn’t overload the server – you shouldn’t get obsessed by this but it’s important to keep the server overload low

How to write such code? There isn’t a specific recipe – every developer has its own style regardless the fact that many use frameworks or follow specific guidelines. So I’m not going to offer you a recipe. Instead of that here are seven of the best practices I follow to write better PHP (and not only PHP) code. If you use them too, you can drastically improve your efficiency as a developer.

1. Use alternative PHP syntax in templates:
I really hope you use templates, to begin with. If you are messing the HTML output directly into your scripts, you need to work on this first. If you already follow the concept to separate your design from the program logic, you are either using some template engine (usually a dead end reducing productivity) or placing a bit of PHP code inside the templates (loops, if/else statements etc). You can add some more cleanness to your views by using the alternative PHP syntax instead of the standard one with “{” and “}”. Using foreach: / endforeach;, if: endif; for: endfor, etc. keeps the PHP code on less lines in the view, helps knowing when a loop is opened and closed and generally looks better.

You can learn more about the alternative PHP syntax on the official P