0

I want to include files in my webpage, but the pages are like this: new1.php new2.php

The number is increasing and i want to include all files with new(a number).php I really don't know how to do that and i can't find it elsewhere.

now i have :

<?php include('includes/new1.php'); ?>
<?php include('includes/new2.php'); ?>
<?php include('includes/new3.php'); ?>
<?php include('includes/new4.php'); ?>

But it is a lot of work to include them all by hand. Is there a way to include the files without doing a lot of work?

Thanks in advance!

Moreno__R
  • 45
  • 6
  • Have a look at [http://stackoverflow.com/questions/3757991/including-a-whole-directory-in-php-or-wildcard-for-use-in-php-include][1] for a couple of approaches to consider. [1]: http://stackoverflow.com/questions/3757991/including-a-whole-directory-in-php-or-wildcard-for-use-in-php-include – KGolding Oct 06 '13 at 21:00
  • `foreach (glob("includes/new[1-9].php") as $filename) { include($filename); }` – Mark Baker Oct 06 '13 at 21:03

2 Answers2

5

This will include 10 files for you.

for ($i = 1 ; $i <= 10 ; $i++){
  include("includes/new{$i}.php");
}

Also, you could specify some numbers in an array if they have no common pattern or order.

$numbers = array(1, 15, 12, 35, 234) ;

foreach($numbers as $number){
  include("includes/new{$number}.php");
}
sybear
  • 7,837
  • 1
  • 22
  • 38
0

There might be better ways to do it, but what comes to my mind right now is using a for loop.Following code supposes you have files 1 to 10 to include, replace the numbers as per your requirement.

<?php

for ($i=1; i<=10; i++)
  {
    $filename='includes/new'.$i.'.php';
    include($filename);
  }

?>
redGREENblue
  • 3,076
  • 8
  • 39
  • 57