Hey guys,

I just wanted to let everyone know that functions can be used very constructively and there are more to them then most think.

Okay this example function is actually pulled from a class of mine so it would probably make more sense in my class but it should make sense anyway.

Right we should all know how to make functions, for those of you that don't I'm going to explain the process.

First off, a function will contain a method or a block of code that we may want to call several times further along in our script. The advantage of adding the block to a function is that we can minimize (so to speak) our code and instead of calling this every time we need to retrieve certain information we use a simple one line call to our function.

[b]Extensive code (padded out a little)[/b]

$userid = "RANDOM_ID";
$query = mysql_query("SELECT * FROM posts WHERE auth_id = '$userid'");
if( @mysql_num_rows( $query ) > 0 ) {
while( $row = mysql_fetch_object( $query ) ) {
echo '' . $row->title . ' Last post on ' . date( "Y-m-D" , $row->time ) . '';
} else {
echo "This user hasn't ever made a post.";


Okay so that as you can see is a fair amount of code to have to write every 5-10mins, so we need to simplify this:

function get_posts() {
$userid = "RANDOM_ID"; ...

scaiferw commented: Nicely written, thanks. +1

This is a heredoc method used for storing large strings that take up more than a line of code.

The PHP documentation can be found here: