note 117124 deleted from language.references.return by sobak
| From: | sobak@php.net | Date: | Thu, 24 Nov 2016 05:41:12 +0000 |
| Subject: | note 117124 deleted from language.references.return by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207543@lists.php.net to get a copy of this message | ||
Note Submitter: Karim dot Khaled dot Wahby at outlook dot com
----
You can also return a function like this :
<?php
function ReturnFunc ($num, $num1)
{
if ($num == 1)
{
// use is to inheret the variable $num1 from the calling function
// You can also Use global to refrance to global vars ;
return function ($num2) use ($num1){return $num1+$num2;} ;
}
else if ($num == 2)
{
return function ($num2) use ($num1){return $num1-$num2;} ;
}
}
function InvokeTheReturnedFunc($num,$num1, $num2)
{
$func = ReturnFunc($num, $num1) ;
echo gettype($func) . '<br />'; // Object
return $func($num2) ;
}
echo InvokeTheReturnedFunc(1, 5, 8) . '<br />'; // 13
echo InvokeTheReturnedFunc(2, 5, 8) . '<br />'; // -3
echo $asdasd ; // would generate a notice
?>