PHP - include a php file and also send query parameters


I have to show a page from my php script based on certain conditions. I have an if condition and am doing an "include" if the condition is satisfied.

if(condition here){
  include "myFile.php?id='$someVar'";

Now the problem is the server has a file "myFile.php" but I want to make a call to this file with an argument (id) and the value of "id" will change with each call.

Can someone please tell me how to achieve this? Thanks.

7/31/2011 2:08:59 PM

Accepted Answer

Imagine the include as what it is: A copy & paste of the contents of the included PHP file which will then be interpreted. There is no scope change at all, so you can still access $someVar in the included file directly (even though you might consider a class based structure where you pass $someVar as a parameter or refer to a few global variables).

8/5/2009 9:25:23 AM

You could do something like this to achieve the effect you are after:


However, it sounds like you are using this include like some kind of function call (you mention calling it repeatedly with different arguments).

In this case, why not turn it into a regular function, included once and called multiple times?

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow