PHP Basic
Introduction to PHP PHP Starting Requirements Downloading XAMPP Local Server PHP Variable Concept and Data Type PHP Coding Syntax Showing Output in PHP PHP String Concatenation Increment & Decrement in PHP PHP Logical Operator and Usage Part-1 PHP Logical Operator and Usage Part-2 PHP Comparison Operators Arithmetic and Assignment Operators PHP Switch Case Statement For loop in PHP Super Global Variable $_REQUEST and $_GET Part-1 String Length in PHP Function Declaration and UsageFunction plays an important role in the programming world. For the discovery of function now it is easy to divide a large code and work with several sections with the help of function. Let's discuss different parts of a function
For writing a function it is needed to use the word [php]function[/php]. And then the name of the function like below(you must need to write PHP code inside <?php ?> block) [php]function calculate[/php]. After that, we need to use first brackets that means parenthesis (in programming language first brackets are known as parenthesis). So let's give parenthesis like below [php]function calculate()[/php] . Finally, it is time to implement body where a function stores and gathers data, process it, give or return the result. For implementing body part you need to use curly braces (in programming language second brackets are known as curly braces) like below [php]function calculate(){ } [/php]
Function parameter and argument are used for sending and receiving data. After receiving data a function may use that or process it. After that function gives the output. For better clearance, it can go through a function declared below
<?php
function employeeManagement($name, $salary, $presence) { //function Declaration
echo "Name is " . $name . "";
echo "Salary is " . $salary . "";
echo "Total working " . $presence."days";
}
?>
If we notice the declared function employeeManagement then we see that there are three variables available inside a parenthesis. This parenthesis is called the argument of a function. Parenthesis receives value stores/process it and after the gives the output. A function may have an unlimited argument. But better practice is to use arguments not more than ten.
A function is valueless until it is called from other places. For knowing more about function calling go here. A parameter is a system of sending data by using variable/directly to a function. After sending data, function receives it, process it and finally give the wanted output. Let's see the below example for function calling to employeeManagement.
<?php
employeeManagement('John Doe','5000$','27'); //function calling
/*output of the function*/
Name is : "John Doe"
Salary is 5000$
Total working 27 days
?>
If we notice parameter section then we see that a function is receiving data, processing it and gives output. But it is not giving any value that can be used to another place. The concept of returning function value is
"when a function receives data, process it and return it to use in any place where it is needed then it is called function return value"
Let's see an example for better clearance
<?php
function additionTwoNumber($firstNum, $secondNum) { //data receiving by argument
$result = $firstNum + $secondNum; //addition process
return $result; //returning value
}
$additionResult = additionTwoNumber(10, 20);
echo $additionResult; //output is 30
//now we will use this 30 to another place
$thirdNum = 30;
$value = $thirdNum + $additionResult;
echo $value; //now result is 60
?
You can also use returning value to any place without storing it to a variable that means directly. See the example
<?php
function additionTwoNumber($firstNum, $secondNum) { //data receiving by argument
$result = $firstNum + $secondNum; //addition process
return $result; //returning value
}
$value = $thirdNum + additionTwoNumber(10, 20);
echo $value; //now result is 60
?
Hope that you have got a better idea about function declaration, usage, and its implementation. Stay with us. For knowing more comment......
If you think you have query... then For contacting with us use Facebook . You can also use Github