A more apt title might be, “How Not To Suck At PHP”… but I digress.
Want to know how to be a good PHP developer? The secret to being good at PHP, and programming in general, is to know what you’re doing. Seriously.
DISCLAIMER: If you think this song… er… blog is about you, you’re wrong. I learned all these lessons the hard way.
Without further ado, here’s the list. Feel free to skim.
#1: Learn HTML
You need to understand the heart of this markup language before you can write something that writes it. That’s right, PHP writes HTML. Whoa.
#2: Don’t Repeat Yourself
D.R.Y. is a pretty powerful concept. Writing the same thing over and over again is terrible. “Didn’t I just fix this yesterday?” It’s the stuff bugs come from.
Write stuff that’s reusable. Or find frameworks that do it for you already. Don’t copy & paste things!
#3: Avoid Spaghetti Code
Are you writing inline PHP? You know. Html, some code, some more html, with a little more code, interspersed with html and code.
If your IDE can’t figure out where one tag ends and another begins, you’ve got spaghetti code. And if you think Dreamweaver is an IDE… just back away. Slowly. Or quickly, if I’m holding a spoon.
Reading elegant code is awesome. Writing elegant code is even better.
#4: Use Version Control
“But I’m the only developer, I don’t need version control.”
Bullshit. If you think you’re never going to have help, this is the sure-fire way to turn that into a self-fulfilling prophecy. Lacking version control pretty much guarantees it.
Have you ever looked at a website (or maybe they/you called it an “app”) and wondered why there’s all those backup files? And renamed folders? And scripts that forcefully exit immediately? Yep, I guarantee there wasn’t any sort of version control.
“Did you overwrite my file? Oh man… do you have a backup?”
If you have version control, you wouldn’t have to deal with that.
#5: Separate Those Concerns
This kinda goes with #3: don’t mix HTML and PHP. It’s gross.
HTML goes into a template. PHP goes into a script. Functions are there to be reused. Classes hold business logic, and do the brunt of the work, including ALL of the database stuff (if you have SQL in your scripts… shame on you).
#6: Learn Context
Understand relative and absolute paths. Your website/application should work even if the location on the server changes.
Variables can overwrite themselves if you don’t pay attention to scope. Or they might not, for the exact same reason.
#7: Understand The Bits
A real web developer knows all these things. They don’t use the “I’m not a _____!” excuse.
If there’s somebody willing to explain stuff to you, listen to them. Especially if they’re more experienced than you are, but even if they’re not: everyone knows different stuff, so even less-experienced people will occassionally show you something new.
Allowing them to explain things helps both: the best way to learn something isn’t to do it, but to teach it. That’s right, a lot of teachers (at least the good ones) are learning more every time they teach.
Try to understand what you’re doing and why. Ask questions. Don’t be so afraid of being wrong, be afraid of creating unmaintainable garbage.