Category:php

Explanation of a Basic PHP Function

What is a PHP function?

In PHP a function represents a block of instructions that perform a task. A PHP Function allows the user to re-use blocks of script without having to rewrite all the code every time it's needed. This is useful for repetitive tasks that perform the same jobs over and over.

PHP has a large number of built-in functions. Thus it is always good to check php.net or Google before writing a function. There may already be one that you can use saving you a lot of time.

Declaring functions

Functions can be placed anywhere in a php document. However, it will make for easy-to-read code if you place your functions at the top of the document or in a separate file.

If you declared your PHP functions in a separate file, the functions must be made available to the main script using one of the include functions such as:

"include_once()". These functions are typically placed at the top of the main php document.

PHP Function Syntax

PHP functions start with the word "function," followed by a name for the function - a name that you give it.

ie. function myfunction(){} Use function names that resemble function's purpose.
Next you use: "()"

followed by:
"{".
Code goes between the curly brackets
"}".

<h3>PHP Function - Example</h3>
^php
function my_first_function()
{
code to do something;
}
?*
Author: D.Shaun Morgan