PHP functions with example || How to create a function in php
Functions in PHP
A self-contained chunk of code known as a function carries out a single purpose. You can directly call one of the many internal or built-in functions in PHP, such as fetType(), print r(), var dump, etc., to complete a certain operation. PHP gives you the option to define your own functions in addition to the built-in ones.
Advantages of using functions
- Within a programme, functions reduce the amount of code that is needed. ---Function enables you to condense frequently used code blocks into a single component. Now, instead of repeatedly copying and pasting the same piece of code, you may do the same task by invoking this function wherever you wish.
- The code is considerably easier to maintain with functions. --- \Any modifications made inside a function immediately applied at all the places without affecting the various files because a function built once can be utilised many times.
- Function facilitates the removal of errors--- When a programme is broken down into functions, you always know exactly which function is to blame for a problem and where to look for it. As a result, rectifying mistakes becomes significantly simpler.
- Other programmes can utilise functions --- due to the separation of a function from the remainder of the script. Simply by include the php file containing those functions, it is simple to reuse the same function in other applications.
There are two types of functions in PHP :
1.internal or built -in functions
2.User defined functions
Rules of naming a user defined function in PHP
- Any amount of letters, digits, or underscores followed by a letter or an underscore begin a valid function name.
- Function names are case insensitive .
- function body enclosed within a pair of braces .the opening curli brace({} including the beginning of the function code and the closing curly (}) brace indicates the termination of the function in PHP.
- PHP begins each function with the word function ().
- functions can also accepts parameters.
- Return value can also be utilised with a function.
Syntax:
function functionName()
{Statement 1 :Statement2 : statement 3 :.....}
Example :
<?php
function myFunction()
{
echo "Example php function";
}
PHP Functions using Parameters
The possibility to pass a parameter to your function is provided by PHP. You are free to pass however many parameters you want. These arguments operate inside your functions like variables. The two integer parameters in the example below will be passed, added, and the result will be printed.
Example:
<?php
function addFunction($number1,4umber2){
$number = number1 + $number2;
echo "the addition of the two numbers are : $number";
}
addFucntion(10,50);
?>
Here the output will be
the addition of two numbers are 60.
PHP default Arguments value
<?php
function sum($a=10,$b=20,$c=30)
{
echo "sum=".($a+$b+$c)."</br>;
}
sum(10,20,30);
sum(20,40);
sum();
?>
PHP functions Returning values
The idea of a "void" function does not actually apply to PHP because all functions in it return a value, even if you don't expressly force them to. Using the return keyword, you may specify what your functions should return.
<?php
function hello(){
return "HELLO WORLD";//No output
}
$txt =hello();//Assigns the value "Hello world " message
echo hello();//Direct Displays "Hello World" message
?>
Example :
<?php
function sum($x,$y){
$z= $x+$y;
return $z
}
echo"5+10=".sum(5,10)/"<br>";
echo"7+13=".sum(7,13)."<br>";
?>
Thanks for visiting our website plz feel free to share your opinion in comment section
Tags:
php