The expression is for calculating the form of a plural string for Gettext. Example:
$expr = 'n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2';
with eval I’m doing something like:
$expr = str_replace('n','$n', $expr);
$n = 5;
$result = (int)eval("return $expr;");
Can this be done without eval too?
Even though it is possible to build a parser that would be able to parse limited amount (you need only trivial math operators to calculate plurals) it doesn’t worth it in terms of time you spend implementing it and performance (obviously it will be slower).
So I’d personally go with
eval()or just implement a function per language you need to pluralize.It is one of the cases when I assume
evalas not “evil” as long as its input is predefined by you