繁体   English   中英

如何在php变量中包含php include statment?

[英]How to include a php include statment in a php variable?

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

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

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

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

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

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

我尝试这样做也没有用:

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

我怎样才能做到这一点? 我很抱歉不是非常专家混合PHP和HTML所以一个小的帮助将不胜感激?

使用输出缓冲区功能:

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

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

http://php.net/ob

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

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

你必须这样做,我相信它是连接调用:

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

要么...

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

简单地说,当我需要回显一些文本后跟一个变量时,我按照以下方式做:

$color = brown
$state = lazy

echo "The quick" . $color . "fox jumped over the" . $state . "dog";

这将得到以下结果:

The quick brown fox jumped over the lazy dog

有关更多信息,请参阅: 连接字符串

试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM