28

is it possible to make something like this?

// file.php
$string = require('otherfile.php');
echo $string;

// otherfile.php
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<?php require 'body.php';?>
</body>
</html>

// body.php
<p>Lorem ipsum something</p>

And get this output?

<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<p>Lorem ipsum something</p>
</body>
</html>

I know that code won't work, but I hope you understand what I mean.

4
  • 1
    See: stackoverflow.com/questions/1683771/… Commented Jan 5, 2012 at 23:06
  • This looks a bit like you are re-inventing templates. Maybe you can use a template engine? Commented Jan 5, 2012 at 23:11
  • I think template engines were re-inventing PHP but it's a matter of opinion :) Commented Jan 5, 2012 at 23:14
  • 1
    PHP is a template engine Commented Aug 31, 2019 at 9:09

5 Answers 5

67

file.php

ob_start();
include 'otherfile.php';
$string = ob_get_clean();
Sign up to request clarification or add additional context in comments.

4 Comments

There is a parse() function on the PHP site at php.net/manual/en/function.ob-start.php that adds error reporting and allows you to pass variables to the template file.
This solution is better than Mark Bakers - if you want to execute PHP code inside otherfile.php
This worked perfectly for me inside my wordpress plugin where I am calling up an include file within a shortcode. Thanks @SmokeyPHP
Awesome and insane in equal parts!
15
$string = file_get_contents('otherfile.php',TRUE);
echo $string

Use of the TRUE argument for file_get_contents() means it will search using the include path, like a normal include or require

2 Comments

If there is PHP in this file it's not going to be interpreted, you would need to use eval or something similar.
Manhim, I'm aware it would need evalling (or prettyfying if being echoed for display).... that was my reading of what OP wanted.
3

Another cool thing to know, but SmokeyPHP's answer might be better:

<?php
$var = require 'myfile.php';

myfile.php:

<?php
return 'mystring';

Comments

1

Yes, you can use a return statement in a file, and requires and includes will return the returned value, but you would have to modify the file to say something more like

<?php
    return '<p>Lorem ipsum something</p>';
?>

check example #5 under include documentation http://www.php.net/manual/en/function.include.php

Comments

0

I need a solution for Joomla and dompdf and I found this solution

ob_start();
require_once JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'file.php';
$html = ob_get_clean();

only with require_once can use all functions from Joomla at the loaded script. The file.php is a .html file renamed to .php and where added php code.

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.