riuppo questo thread per alcune considerazioni: ho fatto un pò di test per capire quale dei due metodi proposti (eval() e eregi_replace()) fosse più "performante".. ebbene i risultati sono tutti a favore del metodo con eregi replace: fino a 20 volte più veloce!!
in media lo script N*2 (eval()) impiega 0,26 sec per completare la stampa, mentre lo script N*1 (eregi_replace()) ne impiega solo 0,014!!
questi gli script con relativi file template
NUMERO 1 - EREGI_REPLACE()
Codice PHP:
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$file_content = implode("",file("template.html"));
$row = "";
for($i="0";$i<"1000";$i++)
{
$row .= "<tr>\n";
$row .= "<td width=\"50\">$i</td>\n";
$row .= "</tr>\n";
}
$output = eregi_replace("", $row, $file_content);
echo $output;
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "
Pagina creata in [b]".round($totaltime,5)."[/b] secondi.";
?>
TEMPLATE.HTML
codice:
<html>
<head>
<title>Prova template</title>
</head>
<body>
<table border="1" width="200" height="200">
</table>
</body>
</html>
NUMERO 2 - EVAL()
Codice PHP:
<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$template = "<html>\n";
$template .= "<head>\n";
$template .= "<title>Prova template</title>\n";
$template .= "</head>\n";
$template .= "<body>\n";
$template .= "<table border=\"1\" width=\"200\" height=\"200\">\n";
for($i="0";$i<"1000";$i++)
{
$var = "$i";
$str = implode("",file("template2.html"));
$str = addslashes($str);
eval("\$str = \"$str\";");
$str = stripslashes($str);
$template .= $str;
}
$template .= "</table>\n";
$template .= "</body>\n";
$template .= "</html>";
echo $template;
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "
Pagina creata in [b]".round($totaltime,5)."[/b] secondi.";
?>
TEMPLATE2.HTML
codice:
<tr>
<td width="50">$var</td>
</tr>
ho postato tutto così volete fare le vostre prove se la cosa vi interessa.. l'output dei 2 script è identico.
cmq bell'articolo!!