Help - Search - Members - Calendar
Full Version: Pass value to an include file in php
HTMLHelp Forums > Programming > Server-side Scripting
Maria T
Hi,

How do you pass a parameter into an include file? I tried the following but it doesn't work.

include "myfile.php?var=123";

and in myfile.php, I try to retrieve the parameter using $_GET["var"].

Any hints? Thanks.
Christian J
QUOTE(Maria T @ Jun 2 2007, 12:51 PM) *

Hi,

How do you pass a parameter into an include file?

You mean write to a file? See http://se.php.net/manual/en/function.file-put-contents.php

QUOTE
I tried the following but it doesn't work.

include "myfile.php?var=123";

and in myfile.php, I try to retrieve the parameter using $_GET["var"].

Didn't understand that: if you've already defined "var=123" in the query string, why retrieve it from "myfile.php"? unsure.gif


Brian Chandler
QUOTE(Maria T @ Jun 2 2007, 07:51 PM) *

Hi,

How do you pass a parameter into an include file? I tried the following but it doesn't work.

include "myfile.php?var=123";

and in myfile.php, I try to retrieve the parameter using $_GET["var"].

Any hints? Thanks.


You do not need to "pass a parameter" into an include file - when you write the "include" in php this literally includes the text of the file at that point. So for example, suppose inside the included file is a reference to a variable $fish, then this will happily access this variable directly, if it is set outside the "include". E.g.

$fish = 'haddock';
...
include 'fishing.inc';

And suppose fishing.inc contains:

<?php // remember you need to add this so the included file is parsed by php
echo "I like $fish";

Then the result will be to print "I like haddock".

(Incidentally, do you know what makes St John's Wood station unique on the London UndergrounD?)

JustChuck
QUOTE(Maria T @ Jun 2 2007, 05:51 AM) *

Hi,

How do you pass a parameter into an include file? I tried the following but it doesn't work.

include "myfile.php?var=123";

and in myfile.php, I try to retrieve the parameter using $_GET["var"].

Any hints? Thanks.


Yes, I finally found the answer for this

include "myfile.php?var=123"; will not work. PHP searches for a file with this exact name and does not parse the parameter
include "http://MyGreatSite.com/myfile.php?var=123"; does work. PHP regards this as a regular URL and parses the parameters.

Hope this helps!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.