Function

A function is a group of statements that will do certain tasks.

Why Function

When we develop a module in a project , we may need to implement certain tasks(block of statements ) more than once . Writing same block of codes more than once is not at all a good practice . Instead we can give a name to that block and can use them whenever necessary . When we define a block with a name , it is known as function .

• Blocks of code that execute in isolation (and local scope) that perform an action
• Function names are case-insensitive; defined in global scope
• Can be referenced before being defined unless function conditional
• Types: built-in (php supplied); user-defined; externally provided

General Format

function funation_name(){
echo “Error”; // can be on or more lines of statements
}

function – a keyword
funation_name – Any desired name(Should be meaningful)

Once the function is desfined , You can can invoke it by calling its name

<?php funation_name(); ?> // It will print the text “ Error”

A function can have arguments . When a fuction is define using arguments in it , we should pass the values when calling the it .

Example :
function function_sum($a,$b){
echo $a+$b;

}
<?php function_sum(7,8); ?> // It will display 15 as the result .

Note : We should not use any Builtin function name as the function name

Leave a Reply

Your email address will not be published. Required fields are marked *