Top Dog - Top 5 Intermediate PHP tips
June 25th, 2008 by Fritz1. Use/Build a framework
If you do more than a few PHP projects you will want to look into frameworks. Yes, at first frameworks are daunting. But they don’t have to be. This is why I like to recommend the idea of building your own. Most likely you already have code and techniques you use regularly that would fit in perfectly. You will also avoid a “learning curve” of using a standard framework. I built my own framework using help from an article over at ONLamp.com: Understanding MVC in PHP. The article is a great introduction to MVC and the framework he builds during it is small and functional. I tweaked it in a variety of ways that worked for me and made modifications to include some of my favorite code/techniques for things like user authentication and database abstraction. Give it a read, maybe it will work for you… if not you can always try some other popular frameworks like cakePHP or Zend. To me these are too bulky with things I don’t want, but to each his own.
So, if you’ve never paid much attention to framework, you’re probably thinking “what’s in this for me?” Frameworks provide a number of things. They give you a common codebase with which to start any site. They usually provide built in user authentication, database handling, and error handling so that you don’t have to rig up each site to use these things. A good framework will establish these things with no extra code from you. They will also provide you with a structure to your site and in an MVC provide logic/display seperation in one way or another. Before long you will wonder how you ever got along without a trusty framework.
My Framework DIslcaimer: Frameworks can be big and scary at first and the concept of MVC even more confusing. I read MVC/framework articles over and over and over before it really sunk in. So don’t give up early, keep reading and keep trying to grasp the concepts. Once you do you’ll realize they really aren’t that bad. Some people may pick the idea up quickly and easily but don’t give up if you don’t get it the first few times.
2. Become a SQL ‘Top Dog’.
PHP (and any programming language) is just a way to manipulate and do cool things to data. But you aren’t giving yourself a fair chance if you don’t properly understand how to get/create that data. GROUP BY, LIMIT, UNION, JOIN, Aliases, Sub-Selects, WHERE EXISTS, Views… all of those things ring a bell right? If not you better get to work brushing up on your SQL. Data should often come to you on a SQL platter. You shouldn’t have to get data out and then manipulate it before it’s even ready to “use”, or use the data you got out to go get more that yo need. A lot of intermediate/beginners make these makes because it’s possible to get by with only a 20% knowledge of SQL. However this will cost you tons of time, effort, extra code, and effeciency in the end. Convinced yet? Get Reading.
2a. Become a database ‘Top Dog’.
A poorly structured database is a painful thing to see. Database normalization along with a solid understanding of keys (primary, foreign), cardinality, and structure can go a long way in making your PHP product better. Just like above… if you are not designing your database right you are introducing tons of extra code and potential bugs into the application.
3. Don’t be afraid to re-invent the wheel.
A PHP master or professional will often tell you “Don’t re-invent the wheel!!!”. They argue that someone has already written code snippets for many of the things you need/want and that you should save time by using them. I disagree with this approach for beginners/intermediate coders. Sure if you’re a pro and money is on the table then time is everything. But theres no better learning tool than writing code. You have so much to learn by coding your own stuff.
This will also help you when the code breaks. We all know we should comment out code because when you come back 6 months later you will have no idea what it was doing. But it is ALWAYS easier for you to debug code you wrote than someone elses, no matter how long its been since you saw it last. Having everything coded in your style will be much easier to think through and fix later. And sure, your attempt may be 20 times less efficient than a snippet from a master coder but when was the last time something you did in PHP dealt with such a load of data and processing that effeciency really became an issue?
4.Master the array
The array is one of the strongest tools in PHP… arrays are fast, easy, non-typed, and easy to pass around. Learning to store your data in arrays and pass/manipulate those arrays well is an important php skill. I find that when dealing with actual application data I often forget about object orientation and instead using arrays to store my “objects.”
Quick example: Pull data from the database into an array… display that array in the form elements on the screen. Using name=’data[field]’ on those form element makes the form itself return an array. From that return I take the resulting data array, pass it off to a function that updates the database based on the values in the array.
Thats a simple example that many novices will spend far too many lines and variables accomplishing. With the simple fact that you put anything in an array, create associative key or numbered key arrays, and pass arrays from one function to another dealing with data has never been easier. I highly recommend writing your own functions that take arrays and parse them into SQL statements for your database. This makes data manipulation easy as pie (mmm I love pie).
5. Don’t be afraid to start things and not finish them
Again if profit for your time is your priority you may not want to follow this… but don’t be afraid to start something. If you feel enthusiastic about an idea or just want to try something… start coding! It may turn into something great or you may never finish it. Don’t worry because either way you will learn things you didn’t know before. I’ve learned 50% of what I know from “play” projects, most of which I never finished. Don’t worry… every bit you learn will help you in the long run. Don’t be afraid to start projects and don’t fret having unfinished projects laying around.
If you’re thinking “hey this dog doesn’t know anything! I came in here for 5 PHP tips and I don’t even see any code!” Well… you should realize that 75% of being a good programmer is planning, theory, and approach. Anyone can hack out some code to do something. But truly writing good code takes a lot more thought and structure than just learning “coding tricks.” So spend some time improving your approach…

The last big piece to discuss is the physical device itself. First off, its slick looking… not dorky and plastic like most bluetooths today. It comes in black, grey, and red for those of you who like it wild. And now there are new thinner models out with a more high scale look. Not only does it wear well but it also fits well. Because of the nipple that needs to stay on your face it has a spring in the ear hook that keeps it pressed to your face, this is nice and i sometimes wish the spring was even stronger to keep it securely in place. There are several different ear hooks (left and right ear) and ear buds (from round ones that sit on top to long thin ones that go down in the ear) so you can find something that fits comfortably for you.
