note 117124 added to language.references.return
| From: | Karim dot Khaled dot Wahby at outlook dot com | Date: | Fri, 17 Apr 2015 21:32:52 +0000 |
| Subject: | note 117124 added to language.references.return | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202292@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 72.52.91.14
Probable Submitter: 197.39.130.225
----
Manual Page -- http://php.net/manual/en/language.references.return.php
Edit -- https://master.php.net/note/edit/117124
Del: integrated -- https://master.php.net/note/delete/117124/integrated
Del: useless -- https://master.php.net/note/delete/117124/useless
Del: bad code -- https://master.php.net/note/delete/117124/bad+code
Del: spam -- https://master.php.net/note/delete/117124/spam
Del: non-english -- https://master.php.net/note/delete/117124/non-english
Del: in docs -- https://master.php.net/note/delete/117124/in+docs
Del: other reasons-- https://master.php.net/note/delete/117124
Reject -- https://master.php.net/note/reject/117124
Search -- https://master.php.net/manage/user-notes.php