views:

1113

answers:

5

Hi

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.

+4  A: 

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

Daff
+2  A: 

You could so 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?

Paul Dixon
I agree, this definitely sounds like it should be a function call instead of an include.
Brenton Alker
A: 

Your question is not very clear, but if you want to include the php file (add the source of that page to yours), you just have to do following :

if(condition){
    $someVar=someValue;
    include "myFile.php";
}

As long as the variable is named $someVar in the myFile.php

Blackethylene
+1  A: 

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 !

Nicolas
ASP tags? really?
Philippe Gerber
Wow, I don't know how I ended up making that mistake...I haven't touched ASP at all, in that century ! Fixing this in a second.
Nicolas
Well PHP supports ASP tags so it is possible :).
rix0rrr
A: 

nice answer Daff i tried to vote but i cannot because i still new in stack over flow.com

hjmelda