3

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>';

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

1
  • include_once is probably not correct in this case since you're doing templating. Use the non-once variant include. Commented Oct 1, 2013 at 12:18

3 Answers 3

7

Use Output Buffer functions:

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

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

See 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

Sign up to request clarification or add additional context in comments.

1 Comment

can you provide a link or something, you might be using it wrong or mix table html elements wrong
0

You have to do it as follows i believe it is call concatenating:

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

or...

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

To put it simply when I ever need to echo some text out followed by a variable the way I do it is to do as follows:

$color = brown
$state = lazy

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

This would give the results of:

The quick brown fox jumped over the lazy dog

For more information see: Concatenate Strings

3 Comments

include returns false or 1, not the content of the file
no, my bad, you can actually return something from include.. interesting (php.net/manual/en/function.include.php)
This won't work anyways. If you have a file test.php include into the variable $table_content and you have an echo "blargh"; in there, it will be printed to the browser, outside of the variable
0

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.