简体   繁体   中英

Create php files from array

The following code when executed will create a file NewFileName.php It will also add content to the page including adding the new file name within the content where I have $pagename.

This all works but my question is can I make multiple files from an array?

So for example


$pagename = array(“fileone”,”filetwo”,”filethree”,”filefour”);


<?php 
error_reporting(E_ALL);

$pagename = "NewFileName";

$newFileName = '../folder/filename-'.$pagename.".php";
$newFileContent = '<?php $pagetitle = "Title '.$pagename.'"; $keywords = "Keywords '.$pagename.'"; $description = "Description about the page'.$pagename.'."; $ads = file_get_contents("../ads/square.php"); include("../include/head.php"); ?>


<?php include("../include/sidebar-anagram.php");?>    

<?php include("../include/foot.php"); ?>';

if (file_put_contents($newFileName, $newFileContent) !== false) {
    echo "File created (" . basename($newFileName) . ")";
} else {
    echo "Cannot create file (" . basename($newFileName) . ")";
}
?>

Yes, you can. PHP offers a way to loop through the given array and do something for each item in that array.

For example:

// Please note that I've changed the variable name to the plural version of the word.
$pagenames = array("fileone","filetwo","filethree","filefour");
foreach($pagenames as $pagename) {
    // Do some stuff here.
}

Have a look here: https://www.php.net/manual/en/control-structures.foreach.php

And also here: https://www.w3schools.com/php/php_looping_for.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM