Jun
17
2013

Getting started with PHP Functions

As I’m sure anyone who has visited this site more than a single time knows, I love php functions!  They make repeated processes and actions a breeze to initiate and take effect without having to copy-paste the same block of code into 25 different pages.  Then g-d forbid you should need to make a change to how it operates, or even worse- it’s just generally terrible and needs a full do-over!

The day I discovered functions was the day I stepped into a WHOLE NEW WORLD of programming.  A world where I no longer had to do the copy-paste-shuffle.  A world where if something turned out to be broken, slow, or just generally terrible, I just opened ONE FILE and updated it.  It was a beautiful beautiful world, and I feel that if you’re actively using PHP (or any programming language for that matter), you will (shakes fist) feel the same!

Getting Started with Functions

There are 2 primary types of functions:

  1. Ones that accept arguments
  2. Ones that don’t take arguments

For example:

function oh_hell_no()
{
    echo 'Oh HELL no!';
}

By calling:

<?php oh_hell_no(); ?>

You will have output to your page “Oh HELL no!”, but notice that the returned value from the function will always be the same.

However, we can pass arguments to functions to alter their output, which is where functions and classes become most useful!

function well_perhaps( $name )
{
    echo 'Well, perhaps Mr/Ms '. $name.' is paying attention!';
}

Now by calling

<?php well_perhaps( 'Bennett' ); ?>

We’ll end up with the statement “Well, perhaps Mr/Ms Bennett is paying attention!” (yes, I am aware that these examples are completely ridiculous!)

And that’s all good and fine and dandy, but WHAT IF WE NEED MORE?! (We ALWAYS need more!)

function well_perhaps( $name, $prefix )
{
    return "Well, perhaps $prefix $name is paying attention!";
}

echo well_perhaps( 'Bennett', 'Mr.' );

//OR assign to a variable since now the function returns a value rather than echo
$statement = well_perhaps( 'Your name', 'Mr/Ms' );

What if arguments should be optional?

Sometimes, and depending on how reusable your functions are, not all of the arguments should be required!

If we try to call the “well_perhaps” function above using only , what we’ll end up with is:

<b>Warning</b>: Missing argument 2 for well_perhaps()

The solution to this is simple fortunately.  By specifying a value in the function argument declaration, (i.e. $prefix = ”), we can simply disregard the value if it hasn’t been passed to the function.

function well_perhaps( $name, $prefix = '' )
{
    //Start the statement since the prefix may not exist
    $string = 'Well, perhaps';

    //Only add the $prefix if not empty
    if( !empty( $prefix ) )
    {
        $string .= ' '. $prefix;
    }

    //And add the rest which we know is persistem
    $string .= " $name is paying attention!";
    return $string;
}

In this manner, we can alter the output of the function, and the $prefix variable no longer needs to be passed (I’ve also accounted for pretty simple logic to ensure the returned value actually makes sense grammatically), and we can simply say…

<?php echo well_perhaps( 'Bennett' ); ?>

That’s it for today!

Leave a comment