Page 1 of 1

Is there a more efficient way to write this?

Posted: Mon Oct 13, 2008 3:30 pm
by someguyhere
This funtions exactly the way it should, but I think there has got to be a better way to write it. Ideas? (FYI - I'm just learning PHP)

Code: Select all

//writes a static page for each URL
$static_html = fopen($url1, "w");
if($static_html!==false)
    fwrite($static_html, "$template");
    fclose($static_html);
$static_html = fopen($url2, "w");
if($static_html!==false)
    fwrite($static_html, "$template");
    fclose($static_html);
$static_html = fopen($url3, "w");
if($static_html!==false)
    fwrite($static_html, "$template");
    fclose($static_html);
$static_html = fopen($url4, "w");
if($static_html!==false)
    fwrite($static_html, "$template");
    fclose($static_html);
$static_html = fopen($url5, "w");
if($static_html!==false)
    fwrite($static_html, "$template");
    fclose($static_html);
$static_html = fopen($url6, "w");
if($static_html!==false)
    fwrite($static_html, "$template");
    fclose($static_html);
 

Re: Is there a more efficient way to write this?

Posted: Mon Oct 13, 2008 4:31 pm
by Reviresco

Code: Select all

$url_array = array($url1, $url2, $url3, $url4, $url5, $url6);
 
foreach ($url_array as $v) {
    $static_html = fopen($v, "w");
    if($static_html!==false) {
        fwrite($static_html, "$template");
        }
        fclose($static_html);
}

Re: Is there a more efficient way to write this?

Posted: Mon Oct 13, 2008 5:20 pm
by someguyhere
Works like a charm! Thank you.