PHP Eval that evaluates HTML & PHP

前端 未结 5 714
北海茫月
北海茫月 2020-12-06 01:22

I\'m messing around with templating and I\'ve run into a situation where I need to echo to the browser a template that contains html & php. How do I evaluate the PHP and

5条回答
  •  生来不讨喜
    2020-12-06 02:24

    Use output buffering instead. eval() is notoriously slow.

    main.php:


    Your file:

    $title = 'Lorem Ipsum';
    $id = 11;
    
    ob_start();
    require_once('main.php');
    $contents = ob_get_contents();
    ob_end_clean();
    
    echo $contents;
    

    The output of this will be:

    Lorem Ipsum

    Greater than 10!

提交回复
热议问题