如何在 php 变量中包含 php include 语句? [英] How to include a php include statment in a php variable?

查看:30
本文介绍了如何在 php 变量中包含 php include 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 html 结构的变量,如下所示:

I have a variable including html structure like following:

$txt = '<table>
           <tr>
               <td>
              </td>
           </tr>
       </table>';

我想在变量中写如下语句:

I want to write the following statement inside the variable :

include_once('./folder/file.php');

我尝试像下面这样写,但失败了:

I try to write it like the following but it failed:

$txt = '<table>
               <tr>
                   <td>';
                   include_once('./folder/file.php');
                  $txt.='</td>
               </tr>
           </table>';

我也这样试过,但还是不行:

And I try it like that and also not work:

$txt = '<table>
                   <tr>
                       <td>
                       {include_once('./folder/file.php');}
                     </td>
                   </tr>
               </table>';

我该怎么做?很抱歉,我对混合 php 和 html 不是很熟练,因此我们将不胜感激 ??

How can I do that? I am sorry not very expert with mixing php and html so a small help will be appreciated ??

推荐答案

使用输出缓冲函数:

ob_start();
include('./folder/file.php');
$include = ob_get_clean();

$txt = '<table>
           <tr>
               <td>' . $include . '</td>
           </tr>
       </table>';

http://php.net/ob

输出缓冲区收集您发送到浏览器的所有内容,直到您清空、删除或结束它.

The output buffer gathers everything you'd send to the browser until you empty, delete or end it.

http://www.php.net/manual/en/function.ob-get-clean.php

这篇关于如何在 php 变量中包含 php include 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆