| Functions |
|
|
|
| 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:
To use this function, all you would need to do is use the function's name, and give it two numbers...
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.
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 codeYou 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:
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
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
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
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:
This will output: 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.
ConclusionUsing 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 |




