Skip to content

Instantly share code, notes, and snippets.

@auxiliaire
Created April 7, 2021 19:03
Show Gist options
  • Select an option

  • Save auxiliaire/78cb1c5cc86401f4cc10688082e5cc3d to your computer and use it in GitHub Desktop.

Select an option

Save auxiliaire/78cb1c5cc86401f4cc10688082e5cc3d to your computer and use it in GitHub Desktop.
Implement `last` in PHP
function last(array | string $xs) {
return match (true) {
is_string($xs) && !empty($xs) => substr($xs, -1),
is_array($xs) && !empty($xs) => $xs[count($xs) - 1],
default => null
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment