| Create Your Own PHP Function |
|
|
|
| Written by Handy PHP Administrator | |||
| Sunday, 13 September 2009 05:27 | |||
|
As you write more and more scripts in any language, you will find that you have to use the same bits of code over and over. For example, in PHP you may use the echo function several times in each script. Now when the creators of PHP developed the scripting language, they realized that the job that echo performs was important and they created the function so that you wouldn't need to explain to the server how to echo every time you want to output data. Without the echo function, you would need to write a large block of code to do the same thing. In PHP like most languages, you have the ability to define your own functions for a script. Lets say that we wanted to print out a list of numbers from 1 to 10. Here is the code we would use without a function: CODE: PHP Snippet
<?php That would output the following: Output:
1
2 3 4 5 6 7 8 9 10 Now if you had to output the numbers 5 to 21, you would have to use the same code and change the starting and ending values for the FOR loop like so: CODE: PHP Snippet
<?php If we had written a function for our first loop script, then we could have used it instead of writing the same code a second time like this: CODE: function number_list()
<?php That would output the following: Output:
1
2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 As you can see, this could save us a lot of time for repeatitive tasks.
|
|||
| Last Updated on Monday, 21 September 2009 22:35 |






