views:

246

answers:

11

Hey all, what are some tips or tricks that new PHP developer's should follow. Good tutorials, best practices etc?

Sam

+8  A: 

Two advices :

Brice Favre
+1  A: 

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!

Raphael Caixeta
+1  A: 

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.

Kerry
-1 for mentioning www.w3schools.com. It has terrible examples.
Yacoby
I'm using tizag.com
Sam
A: 

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!

Liam Spencer
"In this edition, the bulk of the new material covers the latest versions of both technologies: PHP 6 (due out in 2008) and MySQL 5 (available now)."I would not look at this book.
Arkh
Erm, why not? Both technologies are available now are they not?
Liam Spencer
PHP6 definitely isn't available now
kemp
+2  A: 

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.

Sagara
ahahahahahaha! "SO is here saved by his system of vote" - it's indeed funny statement :) Thousands of ignorant lemmings tend to upvote same ignorant answers. Most of voters just cannot distinguish proper solution from a lame one.
Col. Shrapnel
Seems I saw only good ones so, or i got tricked :DAnyway, so is not "saved", but is one of the best I saw anyway. (I edit to fit a little your reality too)
Sagara
+5  A: 

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.

Col. Shrapnel
+1 for getting some general programming knowledge first.
Arkh
... he's not neccesarily new to programming, he just said new to PHP
Mailslut
I'm new to programming ^_^.
Sam
+2  A: 

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!

chimgrrl
+5  A: 

<!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>

0scar
As a PHP veteran this is funny, but I'm slightly scared that the OP may take you seriously.
Neil Aitken
You could add <sarcasm> </sarcasm> to the answer to emphasise the intent of this answer.
Bob Fanger
Nah nah I got it ;).
Sam
@Bob I wish the w3c would hurry up and finalise the sarcasm element.Maybe in HTML6 :)
Neil Aitken
Yeah, this isn't really funny. It would be confusing to the OP if he/she is new to PHP.
letseatfood
@0scar love the new doctype. Genius :)
Neil Aitken
+2  A: 

Use the Netbeans PHP editor, because it understands PHP:

  • It will show parse errors.
  • type s ctrl+space and it will show all functions starting with with "s"
  • It shows what parameters are expected by the function.
  • ctrl+click brings you to the declaration. Making it easy to jump to the implementation of a function or class.
  • etc

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)

Bob Fanger
ctrl+click - woo hooo! been recently assigned to a project with 3400 php scripts and i only have general php experience (luckily good java / delphi skills ). I SPEND DAYS SEARCHING FOR STUFF! Will try the netbeans IDE first thing in the morning tomorra.
Peter Perháč
A: 

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

Purefan
+2  A: 

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:

Object-Oriented Programming With PHP

Also, take a look at stable PHP frameworks.

Sarfraz