Require function with for

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
mustafamisir
Forum Newbie
Posts: 23
Joined: Wed Jun 22, 2005 1:00 pm
Contact:

Require function with for

Post by mustafamisir »

Hi,

I used this part of code;

Code: Select all

for($i = 0; $i < 5; $i++) 
	{
		$id = $temp&#1111;$i];
		require('file.php');		
	}
But this code finish after one turn, how Ican solve this problem?
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

most likely in file.php, you use the variable $i and it throws off the parent loop. Try setting it to $index rather than $i and see if it works
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

From the manual :)

http://us2.php.net/manual/en/function.require.php

Code: Select all

Similarly, looping structures do not affect the behaviour of require(). 
Although the code contained in the target file is still subject to the loop,
the require() itself happens only once.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Using include() instead should solve the problem of it being loaded once.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

require_once(), include_once() is a more appropriate way of dealing with loops I think.
Post Reply