30 seconds of PHP

  Back to Home

once

functionintermediate

Call a function only once.

Return a function, which only calls the provided function, $function, if $called is false and sets $called to true.

function once($function)
{
  return function (...$args) use ($function) {
    static $called = false;
    if ($called) {
      return;
    }
    $called = true;
    return $function(...$args);
  };
}