require/include into variable

前端 未结 9 1578
萌比男神i
萌比男神i 2020-11-27 17:46

I want to require/include a file and retrieve its contents into a variable.

test.php



        
相关标签:
9条回答
  • 2020-11-27 18:22

    You can write in the included file:

    <?php
        return 'seconds etc.';
    

    And in the file from which you are including:

    <?php
        $text = include('file.php'); // just assigns value returned in file
    
    0 讨论(0)
  • 2020-11-27 18:26

    Use shell_exec("php test.php"). It returns the output of the execution.

    0 讨论(0)
  • 2020-11-27 18:35

    If your included file returned a variable...

    include.php

    <?php
    return 'abc';
    

    ...then you can assign it to a variable like so...

    $abc = include 'include.php';
    

    Otherwise, use output buffering.

    ob_start();
    include 'include.php';
    $buffer = ob_get_clean();
    
    0 讨论(0)
提交回复
热议问题