Can you render a PHP file into a variable?

前端 未结 5 1941
孤城傲影
孤城傲影 2020-12-28 08:51

If I have a hello.php file like this:

Hello, !

I would like to do something like this in some php code:

         


        
5条回答
  •  星月不相逢
    2020-12-28 09:24

    As Gumbo said you have to check for the $file variable, its a subtle bug that has already bitten me. I would use func_get_arg( i ) and have no variables at all, and a minor thing, i would use require.

    function renderPhpToString( )
    {
        if( is_array( func_get_arg(1) ) ) {
            extract( func_get_arg(1) );
        }
        ob_start();
        require func_get_arg( 0 );
        return ob_get_clean();
    
    }
    

提交回复
热议问题