1

I use in project such functions:

include ("blocks/myCompanyCommentsListBlock.php");

I need somethig like this:

$str = sprintf("<div>%s</div>", include ("blocks/myCompanyCommentsListBlock.php"));

How this can be done? :) Any links would be useful :)

5
  • do you want the full contents of "myCompanyCommentsListBlock.php" [file_get_contents()] ? or is it php code to run ? Commented Feb 17, 2014 at 21:26
  • 1
    Use functions. Put the code of the include file in a function, include it before the assignment and call the function in the assignment. Commented Feb 17, 2014 at 21:27
  • @jeroen: to be precise - you can return from a file. Commented Feb 17, 2014 at 21:27
  • Can this not be done by echo '<div>'; include 'inc.php'; echo '</div>'; ? Commented Feb 17, 2014 at 21:28
  • This doesn't look like a good idea to me. Commented Feb 17, 2014 at 21:34

1 Answer 1

4

Use output buffering to prevent its content from being sent to the browser. Then use ob_get_clean() to capture its output into a variable.

ob_start();
include ("blocks/myCompanyCommentsListBlock.php");
$content = ob_get_clean();
$str = sprintf("<div>%s</div>", $content);
Sign up to request clarification or add additional context in comments.

2 Comments

@zerkms That's what I was looking for. I had done this once years ago and forgot exactly what it was I did.
Useful to write note to remember this :)

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.