Beginner's Guide to Custom PHP Functions

Save time and code by learning how to use functions in PHP and when to use them.

One of the most useful coding practices you will ever learn in PHP is the use of custom functions. Functions can instantly decrease the amount of time you spend on writing code and thus get more done quicker! In this tutorial, i'm going to explain what functions are and how they can be helpful and used.

What is a function?

We have to start with the basics. A function is a piece of code that can be 'called' to perform a block of code. Functions can have parameters passed to them that can be used in the block of code.

Why use functions?

Do you ever find yourself using a block of code over and over again when you are developing? If so, this is where functions are a great tool. Instead of copying the code over and over, you write it once, then 'call' it by typing the function's name.

Example 1: Echo a Link to My Website

Let's say I find that I link my website name a lot when I code. So, I have decided to write a function to do this for me so I don't have to type the HTML every time.

function my_link(){
   return '<a href="http://www.supadupawebdesign.co.uk" title="Supadupa Web Design">Supadupa Web Design</a>';
}

Now, whenever I want to echo a link to my website in PHP, I just need to call my_link() and it will. That makes that job easier. Plus, if I ever want to change the text, web address or title of the link across my site, all I have to do is edit this function rather than go through all my site and edit every occurrence of it. One important detail to notice is the use of 'return'. With functions, you 'return' the result of the function. So, if you want the function to give you some information back, you must 'return' it. Once a function comes across the 'return' it won't go any further.

Example 2: Bigger Number Function

Let's get a little more complex. What if I use a block of code to compare 2 numbers to find out which is bigger. Because the numbers could be anything, the function will need parameters passed through it. So, here is what my function would be like:

function bigger_number($number1,$number2){
   if($number1 > $number2){
      $message = $number1.' is bigger than '.$number2;
   }
   elseif($number2 > $number1){
      $message = $number2.' is bigger than '.$number1;
   }
   else{
      $message= 'The numbers are equal.';
   }
   return $message;
}

So, the function is called bigger_number(). And I need to pass 2 parameters to it, $number1 and $number2. Here is how I would do this in php:

echo bigger_number(4,5);

And this would return '5 is bigger than 4'.

Useful Ways to Use Functions

Hopefully, with those 2 examples, you will now have an understanding of how functions work. Functions can be useful when doing user input validation, sending emails, providing error messages, uploading images and so much more.

If you have any further questions, or need something further explaining, just leave a comment below and i'll see how I can help you.


Comments

Shahbaz Ahmed Bhatti @ 28th Nov 2011

Please can you tell me, how to make phpmysel function, an dhow to use them
thank

Add a Comment

required
required, but not shared

© 2014 Supadupa Web Design