How do you define a PHP include as a string?

前端 未结 6 1377
我在风中等你
我在风中等你 2021-01-01 12:48

I tried:

$test = include \'test.php\';

But that just included the file normally

6条回答
  •  攒了一身酷
    2021-01-01 13:38

    Solution #1: Make use of include (works like a function): [My best solution]

    File index.php:

    
    

    File included.php:

    
    

    test HTML

    This will output FOO BAR, but Note: Works like a function, so RETURN passes contents back to variable (

    test HTML

    will be lost in the above)


    Solution #2: op_buffer():

    File index.php:

    
    

    File included.php:

    
    

    test HTML

    If you use ob_get_contents() it will output FOO BAR

    test HTML

    TWICE, make sure you use ob_get_clean()


    Solution #3: file_get_contents():

    File index.php:

    
    

    File included.php:

    $foo = 'FOO';
    print $foo.' '.$bar;
    

    This will output FOO BAR, but Note: Include.php should not have opening and closing tags as you are running it through eval()

提交回复
热议问题