As you probably figured out from the title, I'm relatively new to PHP, I'm not PHP retarded, but I'm still in my error prone phase. I would just like to know some good tips to help expedite my progress. I recently learned that it's good practice to totally separate everything from everything else, so keep my html forms in one file, and my form handlers in another and just include the two. That was a good tip. If you cant think of anything, just try to think back to when you were a newbie and what you had to learn the hard way. Any tips would help, thankyou
Don't use the error suppression operator (@
). It's tempting to turn off a pesky notice or warning that you otherwise might not know what to do with, but there's always a better way, and it will come back to haunt you later.
That goes hand-in-hand with doing your development with display_errors set to on for E_NOTICE
error_reporting level. The warnings and notices will let you know if you're doing something that might be considered bad practice (such as not checking if an array index exists before using it).
Note: I (Chacha102) missed stuff! Edit it as needed.
General
Dabble in OOP/Classes. After you've dabbled enough, you'll probably use them in every script.
Session_Start should be called before any white space.
Use a Configuration File. This is a file that is included in the beginning of your script and sets up Constants and PHP Settings before your script runs.
Use Comments in Your Code. You will not remember why you did things a certain way if you come back to your code in a month. That isn't a guess either, its a guarantee.
Using
<?php
and?>
make sure your PHP code works everywhere. That is really important.
Scope
Understand Scope. It means that variables inside functions can't be accessed in variables outside the function.
The Global Scope is basically anything that isn't in a function, class, or (since PHP 5.3) namespace. If you put too many variables in the Global Scope, you end up needing long variable names to seperate everything (You might have multiple variables that hold 'file paths', and they can't all be
$file_path
). You'll understand this more when you get into OOP.Know when Constants should be used vs. Global Variables. Constants are normally things that don't change through the entire script, and should stay ... errr.... constant. IE: Having an Array that holds your script's settings is probably a bad idea.
Reusability
Use functions to do code that you would normally do several times.
Foreach and While are your friends. They really are.
Seperating classes or related functions into individual files makes it much easier to transition between projects that use the same code. IE: I have a single file that contains functions for URL based activity.
Sanitization
Overview: Don't Trust any External Data.
Make sure all POST, GET, COOKIE, and all other data that is automatically generated is what you expect. If you have a 'action' variable in the URL (http://example.com?action=get), make sure that it one of the actions you are expecting. If it isn't, replace it with a default or error.
Sanitize POST, GET, COOKIE Data.
Use MySQLi->Prepare for Database Based Queries. (It makes sure the data is entered right)
Databases
Learn MySQL, SQL, and other database snytax if you want to deal with databases.
Use the proper PHP library for each database type. (MySQLi, PG, etc)
Some people suggest using PDO, as it works with most databases without having to change your code.
Use md5() to hash passwords. (You might not need it immediately, but better to use it early.)
Errors
Have defaults. Always.
Make sure to run with
error_reporting(E_STRICT | E_ALL)
. Get rid of any errors.Use
if
statements to check for certain things to mitigate errors. Example
if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = "default"; }
Never used the
@
suppressor. Just get rid of the errors. If absolutely necessary, use them very sparingly.Develop a way that 'you' code (style), and stick with it. The more familiar you are with how you code, the more likely it is that you can spot your errors.
Learn what the error messages means. Makes debugging a whole lot easier.
Resources
There are Books on Programming
There are Free Books on Programming
There are Tutorials on Programming
There are Books on PHP
There are Tips on PHP
There are Common programming mistakes PHP developers should avoid.
There are IDE's for PHP. (For the Coding Process)
There are tips for debuging scripts.
There are tips for separating PHP and HTML.
The Documentation's FAQ cover 'Frequently Asked Questions', like ones you may have.
Typing
php.net/(any function name)
into your browser will redirect you right to the function's documentation page.There are tons of frameworks/libraries to be used, and chances are you can find one for what you want to do.
StackOverflow has almost 12,000 questions on PHP. Use them.
And as always, There is an Ask Question
button on the top of StackOverflow. Use it.
I hope that helped!
Other Things to Know
Mod_Rewrite: It enables you to make URL's pretty. You should know how it works and how to use
$_SERVER
to get the 'requested' URL (the one shown to the user), and the 'actual' URL(the file that actually runs).Javascript, jQuery: These extend your program into the actual browser. Just glance over the jQuery Docs and know how to include it into your page
CSS: Styles the Page.
HTML: You should know this before writing in PHP, but because HTML is such a small part of your program, you want to make sure the HTML you are putting out is valid. It is the only thing that the browser sees, so it makes a huge difference.
Make sure you have error_reporting
set to E_ALL | E_STRICT
:
ini_set('error_reporting', E_ALL | E_STRICT);
Here's a really helpful list of tips for beginners:
30+ PHP Best Practices for Beginners
Very thorough list, was going to type out some of the tips, but every item in that list is worth reading and applying.
Get a good book or two on intermediate PHP. Read them cover to cover.
I recommend The PHP Anthology and PHP & MySQL Web Development All-in-One Desk Reference For Dummies
And spend a bit of time reading through the official docs at PHP.net. You'll find some really useful lesser-known functions and techniques there.
Follow Chacha102's advice, especially OOP, and then learn Design Patterns, in particular -> MVC. At the end, all you have to do is to be persistent, and realize that it will take time and time... Play with programming :)
Use a framework. If you choose one such as Zend Framework (the one I favour and recommend), it will give you an understanding of OOP and MVC.
Understanding MVC and why it is important will give you a better idea of how and why to seperate html forms, and the PHP form handlers.