A function is a piece of code that can be used repeatedly in a PHP program. A function is mainly of two types:
- Built in Functions; which are already defined in PHP Library.
- User Defined Functions; which are created and defined by users.
Syntax:
function functionName(parameters) { code to be executed; } |
Rules for using Functions:
- Name of a function should not start with a number. It can start with a letter or underscore.
- Name of a function are case-insensitive.
- A function starts with an opening curly brace ( { ) and ends with a closing curly brace ( } ).
- Arguments are the variables, inside the parentheses, after the function name which is used to pass informations to functions.
- For multiple arguments, comma should be used for separation.
- Default argument value can also be specified in a function. If no argument is specified, the function will take the default argument.
- Functions can also be used to return value.
Example
<!DOCTYPE html> <html> <body> <?php function writeMsg() { echo "My First Program using Function!"; } writeMsg(); ?> </body> </html> |
Output
My First Program using Function! |