Functions within Functions
In PHP a function can be embedded within a function. If a function is embedded within a function, then in order to call that function we have to first call the parent function. If we directly try to call the embedded function it will give an error.
In the following example the function func1 is called first and then the function func2 is called. If the function func2 is called without calling the function func1 then it will give an error.
Example: |
<html> |
<body> |
|
<?php |
function func1() |
{ |
function func2() |
{ |
echo "Welcome to expertrating.com! <br/><br/>"; |
echo "func2() is called after calling func1().\n"; |
} |
} |
|
//func1 will be called first. |
|
func1(); |
|
//func2 will be called after calling func1. |
|
func2(); |
?> |
|
</body> |
</html> |
Output

Returning Values
In PHP the values are returned from a function by using the “return” statement. With the use of return any type can be returned including string, lists and objects. The return statement ends the execution of the function and passes back the control to the line from which it was called.
In the following example we will see the use of return statement. In this example a function is written which checks for the smaller value. It returns a “yes” if the first value is smaller than the second one and a “no” if its not.
Example: |
<html> |
<body> |
|
<?php
function lessthan($a, $b)
{
if($a < $b)
{
return "Yes";
}
else
{
return "No";
}
}
$xyz = lessthan(26,22);
echo "Is 26 less than 22?----$xyz";
?> |
|
</body> |
</html> |
Output

|