[SOLVED] Include with Variables

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
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

[SOLVED] Include with Variables

Post by AliasBDI »

I have page with this code:

page.php?domain=name

Code: Select all

<?php $domain = $_GET['domain']; $redirect = "?domain=" . $domain; ?>
<?php include '../includes/mod_users.php$redirect'; ?>
The two PHP codes are actually inside of HTML code, that is why they are separated.

So, when I echo the second line, it comes out like this:

Code: Select all

include '../includes/mod_users.php?domain=name'
That is correct. However, when the actual page runs as an include, the variable is not working, it comes out like:

Code: Select all

include '../includes/mod_users.php$redirect'
What am I doing wrong? The purpose is to pass a variable from include to include, because each include needs a URL variable. Any ideas?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You don't need to add variables onto the end of the included file name. Included files automatically 'inherit' the variables of the file that included it:
Eg

Code: Select all

$foo = 'hello';
include_once 'somefile.php'; //somefile.php can now access $foo as normal
But your specific problem is that you used single quotes here:
<?php include '../includes/mod_users.php$redirect'; ?>
PHP does not interpolate (evaluate) variables inside single quotes, so you need to use double quotes, though as i said above you don't need to 'pass' $redirect to the included file, it automatically has access to it.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Right. It works now. Thanks.
Post Reply