Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php include and parameters

Status
Not open for further replies.
In this case the include file has to address the passed parameters as $_GET.

I always try to include files on the local filesystem and write the code so the functionality can be accessed via a function call. The insertion of passing it through the web server is overhead that can be avoided if the script is on the same system.
 
yes you are right, the include is on the same server, just lazy while testing right now.

yes I try it and no it didn't work.

I have a template from hamweather php and created a custom template to call in the current condidtion into the homepage, works great on the url of the broswer but I want the info to display in a table on the homepage.

tell more about $_get?

thanks

jef
 
so how would I format that?

include($_GET['./weather/hw3.php?zipcode=12345&alt=home_current']);

or something like that?

jef
 
I need to pass two values zipcode and alt (for altemplate)

hw3.php does all the work and display information in a template in the template directory

my custom template is name home_current and the zip code is a real zipcode

works great in the url passing zipcode and template page

but not on the incldue statement

should I define those variables somewhere else?

what do yall think?

jef
 
Have you read the PHP online manual section on variable scope?
Have you read the PHP online manual section on predefined variables?

Where are the values in question coming from?

If they are submitted to hw3.php from a POST-method form, then they will be available to hw3.php and any script it includes in $_POST.

If they are submitted to hw3.php from a GET-method form, then use $_GET everywhere.

If they are being generated and stored in variables instantiated by hw3.php, either use the global directive or references to $GLOBALS.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sleipnir,

Thanks for your help...

so


<?php
$zipcode = 1;
$alt = home_current;
include &quot;h3.php&quot;;
?>
will include the h3.php file in the parent file and configure h3.php to use zipcode 12345 and alt home_current?

I'm not where I can try it, but will asap

right?

jef
 
Your code will work, but you're not quite using the right metaphor here.

When a script includes another file, imagine that you have opened both files in a text editor, copied the code from the included file, and pasted that code into the script that invoked include().

The only difference between cutting-and-pasting and include() is that when the included file is run, PHP switches back to HTML throughput mode. That's why you have to wrap the included file's code in &quot;<?php...?>&quot; tags.

The code you've inserted uses the variable scope of the including file as the variable scope exists that that point in your code.

Three examples, both will include the file &quot;toinclude.inc&quot;:

toinclude.inc:
Code:
<?php
print $a;
?>



script1.php:
Code:
<?php
$a = 3;
include ('toinclude.inc');
?>

When you invoke script1.php, the output returned will be &quot;3&quot;. $a exists in the global scope and is available to the code in toinclude.inc.

script2.php:
Code:
<?php
function foo()
{
   include ('toinclude.inc');
}

$a = 3;
foo();
?>

If you point your browser to script2.php, you will get no output. The included file inherits the variable scope of the inside of the function, and $a is not available. PHP will create a new $a that exists only inside the function and initialize that function to a null value.

script3.php:
Code:
<?php
function foo()
{
   global $a;
   include ('toinclude.inc');
}

$a = 3;
foo();
?>

This one will output &quot;3&quot;, also. Although the included file inherits the variable scope of the function foo, that function now imports $a from the global variable scope to the local function variable scope. Thus, the value will be available to the included file.

This is the reason I generally recommend using the superglobal arrays ($_POST, $_GET, $_SERVER, etc.) whenever possible. These variables are available anywhere because by definition they are a part of every variable scope.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top