Never ever use eval function in PHP !
Updated Jul 31 : we can use Expression Language
In some applications, we need to let user defines the expression (user lambda function) to evaluate a value or a condition. So we store user-defined expression in database then using
eval function and pass variables to that function.
It works, but not securely ! Remember this important thing :
NEVER TRUST USER INPUT !
So how this article will show how we achieve the goal without touching
The idea came out when I used Ansible tool (a Pythonic dev-ops tool, with great docs and community). Ansible uses jinja template engine as mainly syntax for many thing, to render the template file and to evaluate the condition when syntax.
Same in PHP, we have Twig template engine !
This is my library (https://github.com/khanhicetea/twigeval) that using the above idea, one of few libraries which I write Unit Test for ;)
$ composer require khanhicetea/twigeval
Voilà !!! We have user-defined lambda function.
Feel free to contribute ! Thanks ! :)
Til next time !