Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include w/o echo

Tags:

php

I'm coding a web site and I have a php fonction called site_nav.php which first define a $list array and than echos it in a json form.

This way, I can get my web site map with ajax.

My problem is that now, I would like to get the $list object in php, without echoing the json object.

My question: Is there a way to include my php file without activation of the echo call which is in it?

Thanks a lot!

like image 742
malavv Avatar asked Jul 22 '09 19:07

malavv


2 Answers

Yes, just use output buffering. See the documentation on ob_start and ob_end_clean.

ob_start();
include("file.php");
ob_end_clean();
like image 101
TJ L Avatar answered Oct 11 '22 06:10

TJ L


You could use an output buffer to catch the output in a string.

ob_start();

include_once('file.php');

var $output = ob_get_contents();

ob_end_clean();
like image 41
MacAnthony Avatar answered Oct 11 '22 08:10

MacAnthony