[web] pass parameters to php include

Started by
5 comments, last by GameDev.net 18 years ago
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.
Advertisement
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.
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
Quote:Original post by supagu
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


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

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"?>


header.php:
<?function writeheader($title) {?><html><head><title><?=$title?></title></head><?}?>
Don't Temp Fate..
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.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

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 header
include $backend->config['page']; // Should return an absolute path.
// Include footer

This topic is closed to new replies.

Advertisement