PHP / PHP Functions

It is a block of 1 / more lines of code that can be used repeatedly in a program.

Function is of 2 types. They were
1. PHP Userdefined Functions
2. PHP Predefined Functions

PHP Predefined Functions
These are the functions are available in the libraries.

PHP User Defined Functions
These are the functions that which the user has to write the code for it.

Function Syntax
function functionName()
{
Code to be executed;
}

Note
1. Every function may / may not have parameters.
2. The function is used my writing the function name in the code with / without parameters.
3. A function name can start with a letter / underscore but not with a number.
4. Function names are NOT case-sensitive.

Program Output


Hello world!

Explanation
{= Indicates beginning of the function code.
} = indicates the end of the function

PHP Function Arguments
Arguments / Information / Parameters (just like a variable) are passed from calling function to called function. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

Program Output


Number 1
Number 2
Number 3
Number 4


PHP1 Language1
PHP2 Language2

PHP Functions - Returning values
Every function can return a value by using return statement.

Program Output


Sum of 2 numbers: 30



Home     Back