Question about eval in PHP 5

前端 未结 7 1312
误落风尘
误落风尘 2020-12-21 16:44

I have been doing PHP stuff for almost one year and I have never used the function eval() though I know the usage of it. But I found many questions about it in

7条回答
  •  我在风中等你
    2020-12-21 17:14

    eval() is necessary to implement a "compiling" template engine, like Smarty, that uses its own language and compiles it down to php on the fly. The main function of such engines is usually something like

     function render_template($path) {
        $code = file_get_contents($path);
        $php = $this->compile_to_php($code);
        eval($php);
     }
    

    Besides that, everytime you use "include" or "require", you're actually using "eval" under the hood - so, actually, eval is one of the mostly used php constructs.

提交回复
热议问题