# [web] pass parameters to php include

This topic is 4464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how can i pass parameters to an include file in php? lets say i have an id i want to pass to it, usually i could pass parameters on the command line via: myphp.php?id=4 which doesnt work when im in another php script and i want to include myphp.php.

##### Share on other sites
You don't need to pass parameters to include files, think of an include and literally including one file into another there for you can use the variable which are in scope where the include() was called...

for example...

start.php:
<?php// Set myVar to 27$myVar = 27;// Include myinclude.php into start.phpinclude('myinclude.php');?> myinclude.php: <?php// echo the value of a variable called myVarecho 'myVar = '.$myVar;?>

However, I would like to point out that I feel this technique is bad coding practice, it's not wrong and it does work and is completely legal, but I feel you should really have functions/classes in the files you and include and pass the variables in by parameters to the functions/members when you call them rather than rely on variables which are(or maybe) declared elsewhere. Hope that makes sense.

##### Share on other sites
hrmm yeah its really ugly, i'll post my problem:

i have this page, which takes as parameters the body page, and adds on a header and footer automatically. So the header getts written, then my php script is included, then the footer is added on.

Problem here is if i want to redirect users. I cant use the header redirect, unless i use this:
http://au3.php.net/manual/en/function.ob-start.php

which also looks ugly. Any ideas/suggestions appreciated

##### Share on other sites
Quote:
 Original post by supaguhrmm yeah its really ugly, i'll post my problem:i have this page, which takes as parameters the body page, and adds on a header and footer automatically. So the header getts written, then my php script is included, then the footer is added on.Problem here is if i want to redirect users. I cant use the header redirect, unless i use this: http://au3.php.net/manual/en/function.ob-start.phpwhich also looks ugly. Any ideas/suggestions appreciated

My approach to this problem is not to output anything to the browser until you have completed all the logic and therefore know if you're going to do a redirect or display a page. Since I'm also someone who belives that the code and the html should not be mixed I always use some sort of templating system (either write my own, use Smarty or use XSL), Smarty is a very good complete templating system and is available from http://smarty.php.net/ check it out if you're working on reasonable sized projects!

I don't know if I've really answered you're question but hopefully provided some food for thought ;)

##### Share on other sites
A common solution to this making the whole include a function.

Example:

MyPage.php:
<?phpinclude("header.php");writeheader("My PHP Page"); //Write page with Title "My PHP Page"?>

<?function writeheader($title) {?><html><head><title><?=$title?></title></head><?}?>

##### Share on other sites
Even easier is an output buffer:

ob_start();// do stuff// output stuff// include stuff// do more stuff// output more stuff// Hey, I can still do a header() redirect here!$contents = ob_get_contents();echo$contents;

The "output stuff" doesn't actually output anything. It's captured by the output buffer (ob). This means that you can still do header() redirects at the end of the script. The last two statements actually output all the captured contents.

##### Share on other sites
Yuck. A far better approach is to follow xstreme2000's advice and implement your own simple management system, or to use an existing one.

$backend = new Backend(new MyDatabaseImpl);$backend->run(); // Does pre-processing for later use:
// session management, grabs the requested
// page, etc.

if (is_empty($backend->config['page'])) header('HTTP/1.0 404 Not Found'); // Supposed redirect. include 'template/mytemplate.phtml';$backend->shutdown(); // Shouldn't be required.

Then...

// mytemplate.phtml

include \$backend->config['page']; // Should return an absolute path.
// Include footer

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999981
×