Is there a more efficient way to write this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Is there a more efficient way to write this?

Post 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);
 
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

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

Post 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);
}
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

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

Post by someguyhere »

Works like a charm! Thank you.
Post Reply