How to Write PHP functions Part 1
Explanation of a Basic PHP Function
What is a PHP function?
In PHP a function is a piece of programming code that web developers use to perform a task -- usually a repetetive task. A PHP Function allows the user to re-use blocks of script without having to rewrite all the code everytime it's needed.
PHP comes with a lot functions already built into the PHP installation. So, before you decide to write a function, you should check the php manual. The function that you are looking for just might be there. After looking the PHP manual, you still are not able to find the PHP funciton that you need, then it's time to write your own function. A beginner php programmer, you will soon learn that functions are everywhere, and you really can't do a lot without them!
Declaring functions
Like other programming languages, PHP functions have to be declared. To declare PHP functions programmers either write the function at top of the document where the function will be used, or in a separate file. If the PHP function is declared in a separate file, the programmer has to make the function available to his/her script by using another php function such as include_once();.
PHP Function Syntax
All PHP functions start with the word "function," followed by a name for the function. It is suggested to use names that resemble what the PHP function does. Next, there will be "()", followed by "{". Now some code to do stuff, and finally another "}". Lets take a look at a declared php function.
Example PHP Function
<php
function my_first_fucntion(){
code to do something;
}
?>