Inlcude files in a loop, with variable as file name?

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
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Inlcude files in a loop, with variable as file name?

Post by Kadanis »

I'm a bit of a PHP noob. Been using it for about 9 months and still learning so sorry if this is stoopid ;)

my question is, is it possible to iterate over includes like in the below example

Code: Select all

for ($a = 0; $a < 10; $a++){
 include_once("file$a.php");
}
the reason i ask is that some code i'm working on needs profiles and i'd like to store them in separate files for easy maintanence. the thing is that more profiles could be added on an ad hoc basis as they are required and i don't want to have to edit the main code every time one is added.

thanks in advance for help/advice
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Yes, it is possible.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you might be interested in file_exists()
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

thanks for the replies. and yes, i'll be using file_exists to make sure that the file is there before including ;)
Post Reply