Write a PHP program to call a given function only once.
<?php //Licence: https://bit.ly/2CFA5XY function once($function) { return function (...$args) use ($function) { static $called = false; if ($called) { return; } $called = true; return $function(...$args); }; } $add = function ($a, $b) { return $a + $b; }; $once = once($add); var_dump($once(10, 5)); var_dump($once(20, 10)); ?>
Sample Output:
int(15) NULL
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer