繁体   English   中英

从数组创建 php 文件

[英]Create php files from array

以下代码在执行时将创建一个文件 NewFileName.php 它还将向页面添加内容,包括在我拥有 $pagename 的内容中添加新文件名。

这一切都有效,但我的问题是我可以从一个数组中制作多个文件吗?

所以例如


$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) . ")";
}
?>

是的你可以。 PHP 提供了一种循环遍历给定数组并为该数组中的每个项目执行某些操作的方法。

例如:

// 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.
}

看看这里: https://www.php.net/manual/en/control-structures.foreach.php

还有这里: https://www.w3schools.com/php/php_looping_for.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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