We help IT Professionals succeed at work.

Capture output of php script into a variable?

raterus
raterus asked
on
Medium Priority
1,651 Views
Last Modified: 2011-09-20
Hi,

I'm trying to compare the features of PHP to ASP.NET.  One thing I do all the time in ASP.net is execute another page, grab the content, and use it in a variable.  I'm trying to figure out how to do this in PHP.

Lets say I have the following two files:
main.php
subcontent.php

In main.php, I want to execute subcontent.php, and grab the output into a variable.  I'd rather not have to mess with URL's, which most of the examples I see involve doing.  I just want to execute subcontent.php, and get all the output to a variable.  This is not the same thing as using "include"!

Thanks for any help,
Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:

ob_start();
include "subcontent.php";
$variable = ob_get_contents();
ob_end_clean();

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2005

Author

Commented:
That definitely looks good to me, Thanks!

Might want to chime in on this one too if you'd like,
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_22038506.html
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
I use that method a lot in my web pages, works fine.

Glad I could help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.