Skip to content

Instantly share code, notes, and snippets.

@sergioatanacio
Created November 8, 2021 04:38
Show Gist options
  • Select an option

  • Save sergioatanacio/5080798dfed148009550b4237cd89dde to your computer and use it in GitHub Desktop.

Select an option

Save sergioatanacio/5080798dfed148009550b4237cd89dde to your computer and use it in GitHub Desktop.
<?php
if(! function_exists('lastElement'))
{
function lastElement($lastElement)
{
if(!is_array($lastElement) || $lastElement == []){
return [];
} else{
return $lastElement[count($lastElement) - 1];
}
}
}
if(! function_exists('popArray'))
{
function popArray($popFunction)
{
if (is_array($popFunction)) {
array_pop($popFunction);
return $popFunction;
} else {
return [];
}
}
}
function operation(string $operator, array $values, $result = null)
{
if($values === [])
{
return $result;
}
else
{
if ($result === null)
{
$values = array_reverse($values);
$result = lastElement($values);
$values = popArray($values);
}
$sendEvalResult = eval('return '.$result.' '.$operator.' '.lastElement($values).';');
return operation($operator, popArray($values), $sendEvalResult);
}
}
echo(operation('.', [9, 2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment