Sign in to follow this  

[web] pass parameters to php include

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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.php
include('myinclude.php');
?>


myinclude.php:

<?php
// echo the value of a variable called myVar
echo '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 this post


Link to post
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 this post


Link to post
Share on other sites
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 ;)

Share this post


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

Example:

MyPage.php:

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






header.php:

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this