1

Alright this probably sounds easier than it is :)

I've got two files:

file1.php
file2.php

Withing my file1.php is a loop and within that loop i'd like to execute (not include) file2.php.

while(1){ //execute file2.php }

I know i could use exec() or wrap file2.php into a function but is there a native function to do something like this?

3
  • 2
    Why do you want php to execute php? Commented May 25, 2010 at 13:29
  • i need to quickly develop a testing environment and this is the most realistic approach to simulate something :) Commented May 25, 2010 at 13:39
  • include will execute the script. Commented Dec 24, 2012 at 23:30

3 Answers 3

4

And why shouldn't an "include()" work?

If you need, you could perform some ambient sanitization or initialization before. If you included the file in a function's body, for example, all the local variables would be masked. You'd still need to move session contents, for example, but that's easy...

Otherwise, you could still launch the php interpreter (exec php passing it the script as argument). It's easier to make a wrapper bash script for that.

Sign up to request clarification or add additional context in comments.

Comments

1

file_get_contents() should do the trick.

3 Comments

NO. use require or include.
@maček OP specifically said not to use include, so I just listened. I'd say your -1 isn't justified.
(although I may agree Palantir's answer is a better solution)
1

If really necessary, you can use backticks. E.g.:

$output = `php file2.php`;

But I would encapsulate the functionality in file2.php in a function, include the file once and run this function in your loop. This is a much cleaner approach imho.

3 Comments

thanks, didn't even know there is anything like that in php :)
why wouldn't you use require or include?
@maček: The OP explicitly stated to not include the file. includeing would load all the symbols of that file into the file where it is included to, which might cause collisions and what not. Calling php file2.php seemed to be a really easy way to still execute the file and avoid those problems. But I also mentioned that with proper structure of the file, an include would be a more appropriate solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.