Hey all, what are some tips or tricks that new PHP developer's should follow. Good tutorials, best practices etc?
Sam
Hey all, what are some tips or tricks that new PHP developer's should follow. Good tutorials, best practices etc?
Sam
Two advices :
The way I learned PHP (5 years ago) was by jumping on a project that I had no idea on how to accomplish and go about figuring out all of the details. I created a little project social network from the ground up without knowing ANYTHING by just following tutorials and reading around. I actually have the source code posted on GitHub.
What I wish I had started with was with an MVC framework, like Zend, CakePHP or CodeIgniter just for the sakes of them having security and organization in mind. Hope that helps you get started!
I disagree with the framework idea and I think you should learn the essentials of PHP and then learn a framework, if you are so inclined.
I would follow the tutorials at www.w3schools.com
Then, the best I consider learning is to give yourself a project (similar to how Raphael said he jumped on an project with no idea how to accomplish it).
Learn how, find tutorials, ask on SO, other places, etc., to get that project done. Make it a project that you actually want done, perhaps a resume page, perhaps a simple service, something that you actually care about, but won't matter if you mess up.
PHP.net is your friend and has great documentation on every function.
I would definitely look at this book here.
Also, as the book suggests, learn MySQL alongside PHP. That way you learn how to implement dynamic web applications. I would learn to program PHP without the frameworks at first so you get a better idea as of how the technology works. Frameworks then become more useful. Good luck!
Some tricks:
When you learn Php, you must be really careful about informations you find. The documentation (Doc) is usually right. But internet sources aren't.
Actually, lot's of "developer" (are they ?) think they find tricks, post them, and finally the community and helps channel are full of mistakes (SO is not the worst with its system of vote).
There are really a lot of information on the documentation, and they are more functions than you can initially think. Use them. (as in all languages indeed)
I agree with other answers. To learn good, you must be involved in a big (or not) project, but something that will allow you to learn and practice. Using a framework is for me only the "Part 2" of the learning. You can't use all capacity of a framework if you have no idea about how it's implemented. But you must do it of course. As long as you understand what you do, it's useless to redo always the same thing.
Forget PHP and learn programming in general: variables, conditions, loops, arrays, OOP, debugging, profiling. Relational database principles knowledge is obligatory too.
Most of PHP developers suck just because lack of this basic knowledge. Do not follow their way by learning some common patterns and "best practices".
Note that framework is not PHP, it's just another language. It is dangerous to learn some framework before getting knowledge on how it works. One may become a mindless robot assembling lego bricks, not a programmer.
I think one thing that really helped me when I was starting out in PHP/MySQL was that I initially wrote using a text editor instead of one of those IDE's that makes you lazy by suggesting method names.
I also agree with Liam Spencer, I have never developed a php web app without the use of mySQL so it's really important to learn your SQL Statements. www.w3schools.com not only php tutorials but also a SQL tutorial also.
The other thing is nowadays writing PHP in an Object Oriented style is really important so if you don't come from a java or c++ background (or any other OOP languages, for that matter) then I would really suggest you brush up on OOP concepts. If you're preparing a career in PHP nowadays most developers I know develop in OOP.
Try to write a few apps in php, and remember to //COMMENT!!! Woe is the developer that touches code that hasn't been commented or has good documentation. And if you're coding by yourself vs. a team, it's just a good habit to comment.
It might be unrelated, but I also find myself using json, jquery, and ajax in general alot in cooperation with my php web apps. It wouldn't hurt to learn those either.
Hope this helps!
<!doctype html6>
<sarcasm>
Best practices:
Try to mix naming conventions. Example: Name some functions like readData()
and some like add_member()
and some like removemember()
. That way your code will blend in better.
Never ever make your app check whether magic quotes are on or off. Then release it to the public and turn off your cell phone and switch email address and move to Paris.
PHP has a cool function wich allows you to have a function called each time PHP encounters a new class-name. You can use this to autoload code. For instance put your graphics functions in gfx.php
and by using autoload you can load your file by just typing $a = new gfx()
. Really cool. (You don't need to use OOP to benefit from this. But you should at least create an empty class named gfx
or it won't work.)
If you want to keep a user logged in you can just store the password he entered when logging in as a cookie. That way you can check his password every time he surfs to a new page.
Create one class called Controller
, one called Model
and one called View
. It looks professional.
If users can enter text, like on a forum, then all you need really is this simple regexp which removes those <a>
-tags: preg_replace("|<a.*</a>|", $text)
. It's been tested by thousands of web sites so its very secure.
AJAX is really cool because you can have the browser construct the SQL-statements in javascript, and then send them to PHP which can then execute the queries. This is like the SETI-program—distributed computing! You move some of the load from your server to the clients. Nifty.
There's tons more, but I'm sure you'll find out lots on your own ; )
</sarcasm>
Use the Netbeans PHP editor, because it understands PHP:
ctrl+space
and it will show all functions starting with with "s"ctrl+click
brings you to the declaration. Making it easy to jump to the implementation of a function or class.In short: It actively tries to help you write code.
I prefer to learn with a fast feedback loop. So i'm pleased with the editors instant feedback.
However its helps to go to http://php.net/[function name] for a deeper understanding of the function and get some great tips from the comments.
(I prefer netbeans over Eclipse PDT based editors, but those are also quite good)
code code code and then code some more, nothing teaches better than practice. Make mistakes and look for how to fix them, dont immediately ask in a forum or such but try on your own first
First and most important, the security:
PHP Security Guide
Another Good Security Guide
Then you should go for at basic level:
And then, you must learn OOP and most likely frameworks: