[PHP] Includere un file in una varabie!

Posted on 29. Nov, 2009 by daniele in php, tutorials


Mi capita spesso di dover includere un file solo se si verifica una condizione o comunque di variabilizzarlo in modo da gestirlo al 100%. Per fare ciņ utilizzeremo le funzioni ob_start(), ob_get_contents() e ob_end_clean(). Qui di seguito trovate un esempio per realizzare quanto detto!

 
< ?php
//  Filename: INDEX.PHP
//  ==========================================
 
//  all the usual page and template code here
//  and where we want out 'include' variable to
//  appear, we add these lines
 
ob_start(); # start buffer
include_once( '/home/user/public_html/ad_table.php' );
# we pass the output to a variable
$html = ob_get_contents();
ob_end_clean(); # end buffer
# and here's our variable filled up with the html
echo $html;
 
//  we can then continue on with our web page and template
//  script or even add another 'include' variable!
 
ob_start();
require_once( '/home/user/public_html/ad_table2.php' );
$html2 = ob_get_contents();
ob_end_clean();
echo $html2;
 
?>
 

Correlati

Leave a reply