Functions PDF Print E-mail
User Rating: / 0
PoorBest 
PHP - Basics
Written by Stuart Duncan   
Friday, 16 October 2009 20:50

Some argue that this topic is more for intermediate PHP developers as there is a lot to it, but I beg to differ. I think you're ready and because you are ready, I want to teach you about functions. They are extremely powerful and what's more, will be the stepping stone that leads to you doing Object Oriented Programming. Sound scary? Don't worry, if I can do it, so can you.

 

If you've read the previous 2 articles of mine, then you already get the concept of creating and using variables and you get the idea of reusing something over and over and over again by including a separate file such as a header and/or footer.

 

Functions are the second greatest weapon that you as a PHP person can use... the first is for another article, and it will be in the intermediate category.

Functions do a few things very well:

  • Make commonly used code accessible in a lot of places while only writing it once
  • Separate a piece of code from the rest of your code so that it exists on it's own
  • Give it some information and get some information back


To give you an easy example of how a function could be used, we'll do some basic math first.

function doMath( $num1, $num2 ){
$answer = $num + $num2;
return $answer;
}

To use this function, all you would need to do is use the function's name, and give it two numbers...

<?php
echo doMath( 2, 3 );
?>

This will display: 5

It goes to the function, uses 2 as $num1 and 3 as $num2 and does what it finds inside the { } brackets and will return the $answer. You can see how this would make life a lot easier if you had to do addition on a page over and over again but didn't want to have to write out the equation all the time.

Let's do a more practical example that can really demonstrate how this would become very useful.

function money( $number ){
return '$' . number_format($number, 2, '.', ',');
}

I personally use this function in a lot of my scripts as it makes it very easy to display a number in a money format. Imagine coding a page that displays an invoice, 3 items each, a sub total, taxes, shipping and a total price... it really would be annoying to right out '$' . number_format($number, 2, '.', ',') each time when just putting money( 1234.99 ) is so much easier.

By the way, money( 1234.99 ) returns the value $1,234.99.

Now imagine the person you're doing this invoice for doesn't like having comma's in their numbers. You could either go back to the items, taxes, sub total, shipping and total prices and change all of those... OR.. you could change your one function and be done.

 

Separation of code

You may have noticed that in the examples above, you have to "pass a variable" to the functions, like the money function has the number. The reason for this is that a function is truly separate from the rest of your code in that you can't just use any variable you wish. I'll give you an example:

$number = 10;
function money(){
echo $number;
}

This won't work... and the reason is that the variable $number in the function money does not exist. It exists outside of the function, but not inside. There are 3 ways to have that variable be in the function...

Method 1 - Pass the variable

function function_name( $variable_name ){
echo $variable_name;
}

This method is most common as find that a lot of times you don't really pass it something that is set one time on your page, like the money example from above. You pass it a lot of different values a lot of different times.

Method 2 - Global variables

function function_name(){
global $variable_name;
echo $variable_name;
}

In this case, perhaps you do have something set one time in your code, such as $variable_name = 'my website'; and you want your title to be available within the function, you can declare it a global variable within the function. "global" means, available anywhere.

Method 3 - Local variables

function function_name( $variable_name ){
$output = 'The variable I am using in this function is ' . $variable_name;
echo $output;
}

The variable $output is a "local variable" because it is created in the function, and used in the function, and that's it. This means that if you were to write echo $output; outside of the function, it wouldn't work. The variable $output does not exist outside of the function.

That is not to say that you can't have one outside of the function.... but it would have a separate value as it would be "separate" from your code. I'll give you an example:

$output = 'This is a variable outside of the function<br>';
function display(){
$output = 'This is a variable inside of the function<br>';
return $output;
}
display();
echo $output;

This will output:
This is a variable inside of the function
This is a variable outside of the function

You'll notice that both of them have different values, they exist separately because one is inside a function and the other is outside the function.

 

Conclusion

Using variables is a coder's solution to doing something one time instead of many times.... using include/require statements are coders way of including one file or piece of a file over and over again... finally, functions are a coder's way of doing a chunk of code one time instead of over and over again. Let's face it, it would be nice to pick up a few PHP tutorials and be able to create a shopping cart system but for 90% of people out there, the biggest reason to learn PHP is to make those "over and over and over again" tasks much more streamlined and much easier to not only create but to maintain later... and it's for this reason that I have started with variables, includes and functions as the first 3 articles. Try them out and practice as much as you can. From here on, the tutorials get into the nitty gritty of dealing with PHP's core functions, creating functionality and eventually, building that shopping cart system!

Last Updated on Saturday, 17 October 2009 06:13
 

Add comment


Security code
Refresh

Login to TyCamTech.com

Donate

Must have Pepsi, feed my addiction here:


Recommended Books

Introduction to Programming
A Quick And Easy Ebook That Introduces Readers To Computer Programming In A Jargon Free, Easy To Understand Way.

Setting up a Web Server
Teaches All The Steps To Hosting A Website From Home.

How To Flip Websites Home Study Course.
Complete Video Instruction On How To Quickly Set And And Flip (i.e. Sell) Websites For Profit. 6 X Video Lessons Plus Bonuses -- Great Value For Buyer And Maximum 75% Payout For Affiliate!

Follow us on



Follow tycamtech on Twitter