PHP - include a php file and also send query parameters PHP - include a php file and also send query parameters php php

PHP - include a php file and also send query parameters


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).


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

$_GET['id']=$somevar;include('myFile.php');

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?


An include is just like a code insertion. You get in your included code the exact same variables you have in your base code. So you can do this in your main file :

<?    if ($condition == true)    {        $id = 12345;        include 'myFile.php';    }?>

And in "myFile.php" :

<?    echo 'My id is : ' . $id . '!';?>

This will output :

My id is 12345 !