-2

私はここにこのコードスニペットを持っています:

<?php
include('a.php');
include('b.php');
include('c.php');
include('d.php');
include('e.php');
?>

このファイルをループして、これらの各ファイルを1回実行したいと思います。どうすればこれについて行くことができますか?私はphpを初めて使用します。

4

2 に答える 2

2

あなたはこのようなことをすることができます...

<pre>
<?php 
set_time_limit (0);
$files = array('a', 'b', 'c', 'd', 'e',);
foreach($files as $f){
    include_once $f.".php";
    echo "finished file $f\n";
    flush();
}
?>
</pre>
于 2013-02-01T05:01:55.733 に答える
0

あなたはこのようなことをすることができます:

class Loader
{
   protected $files = array();

   public function __construct($files)
   {
       $this->files = $files;
   }
   public function Init()
   {
      foreach($this->files as $file)
      {
          if(file_exists($file)) 
          {
             require_once($file);
          }
      }
}

$loader = new Loader(array("a.php", "b.php","c.php","d.php"));

$loader->init();

ファイルがどこにあっても(質問にリストされているすべてのファイルを必要とするファイルがであると仮定しましょうindex.php)。

require、またはincludeこのファイル(Loader.php)をファイルに入れたいと思うでしょうindex.php。例えば:

index.php

<?php
 // file index.php
  require_once('Loader.php');

  $loader = new Loader(array("a.php", "b.php","c.php","d.php"));

  $loader->init();

?>

この設定により、index.phpファイルでオブジェクトを作成できるようになり、コードがはるかに整理されてモジュール化されます。

于 2013-02-01T05:27:11.173 に答える