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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to use INCLUDE to password parameters?

Status
Not open for further replies.

seahorse123

Programmer
Jul 25, 2005
39
0
0
US

If I want to display the included file's information based on passed value, is it possible to do that?
for example:

file1.php:
<?
$a=$_GET['p'];
if ($a==1) {
print "ABCD";
}
elseif ($a==2) {
print "XYZ";
}
?>

file2.php:
<?
...
include "file1.php?p=1";
...
include "file1.php?p=2";
...
?>

so file2.php will display "ABCD" and "XYZ", if this cannot be done, how to use external function which is located in different file?

Thanks.
 
The notation "?p=1" only makes sense within the context of an HTTP request. If you are using filesystem includes, this will not work but instead will generate an error, as your filenames to not include "?p=1".

It is possible, depending on your PHP configuration, to do HTTP includes:

include ('
But I do not, repeat DO NOT, recommend doing this to include a script on the same filesystem. It's slower and much more server resource-intensive.

Anyway, it's probably not a good idea to get into the habit of including files multiple times if for no other reason than rerunning identical code multiple times goes against the entire reason for subroutines.

I recommend that you in file1.php create a user-defined function, include the file once, then invoke the defined function twice with two different inputs:

file1.php:
Code:
<?php
function foo ($a)
{
	switch ($a)
	{
		case 1:
			print "ABCD";
			break;
		case 2:
			print "XYZ";
			break;
	}
}
?>

[BTW, elseif is evil and should never be used. It adds nothing unique to the language and if overused it can reduce code readability. I took the liberty of rewriting your code using a switch()]

Then in file2.php:
Code:
<?php
include ('file1.php');

foo(1);

foo(2);
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top