PHP FUNCTION
A function is ablock of code that can be executed whenever we need it.
We are the web developer team in our company.
A function is ablock of code that can be executed whenever we need it.
Creating PHP functions:
All functions start with the word "function( )".First we want to name the function because already there are 1000 built-in functions are available.
Example:
<html>
<head>
<title>Writing PHP functions</title>
</head>
<body>
<?php
function writeMessage() {
echo "We are the web developer team in our company.";
}
writeMessage();
?>
</body>
</html>
Output:
We are the web developer team in our company.
PHP Functions with Parameters:
In PHP we can able to pass the parameter in functions.We can pass parameters how much we want.These parameters working like a variables in a functions.
Example:
<html>
<head>
<title>Writing PHP functions with parameters</title>
</head>
<body>
<?php
function addFunction($num1,$num2)
{
$num=$num1 + $num2;
echo"Sum of the two numbers is: $sum";
}
addfunction(20, 30);
?>
</body>
</html>
Output:
Sum of the two numbers is:50
PHP Function Returning Value:
Not only in PHP, In all functions side functions return a value by using return statement with value or object.
We can return a 'n' number of value from a function using retun array(a,b,c,d).
<html>
<head>
<title>Writing PHP functions which returns value</title>
</head>
<body>
<?php
function addFunction($num1,$num2)
{
$num=$num1 + $num2;
return $sum;
}
$return_value =addFunction(20, 30);
echo "returned value from the function: $return_value";
?>
</body>
</html>
Dynamic Function Calls:
We can able to assign function name as strings to the variables and then manage these variables
exactly to the function name itself.
Example:
<html>
<head>
<title>Dynamic Function Call</title>
</head>
<body>
<?php
function sayHi()
{
echo "HI <br />";
}
$function _holder="sayHi";
$function _holder();
?>
</body>
</html>
Output:
HI
No comments:
Post a Comment