First, have a look at Pro PHP Security by Chris Snyder and Michael Southwell from Apress. It will tell you all you need about encryption. There's a lot there.
From there you can download the source code for the book and look for a file called mcrypt.php in Chapter 6. It also comes with a sample implementation.
Good luck! And if it's useful to you, buy the ebook or dead tree version of the text. It's very informative and will teach you a lot! I'm not affiliated with Apress or the authors in any way, I'm just someone that found the book extremely useful.