Home » Php » php – How to pass parameters with `include`?

php – How to pass parameters with `include`?

Posted by: admin July 12, 2020 Leave a comment


I have a PHP script called customers.php that is passed a parameter via the URL, e.g.:


Inside my customers.php script, I pick up the parameter thus:

$employeeId = $_GET['employeeId'];

That works just fine. I also have an HTML file with a form that inputs the parameter, and runs another php script, viz:

<form method="post" action="listcustomers.php">
  Employee ID:&nbsp; <input name="employeeId" type="text"><br>
  <input type="submit" value="Show">

Then in my listcustomers.php script, I pick up the parameter so:

$employeeId = $_POST['employeeId'];

So far so good. But his is where I am stuck. I want to run my customers.php script, and pass it the parameter that I pave picked up in the form. I am sure this is really simple, but just cannot get it to work. I have tried:

include "customers.php?employeeId=$employeeId&password=password";

But that does not work. Nor does anything else that I have tried. Please help me.

How to&Answers:

You can’t add a query string (Something like ?x=yyy&vv=www) onto an include like that. Fortunately your includes have access to all the variables before them, so if $_GET['employeeId'] is defined before you call:

include 'customers.php';

Then customers.php will also have access to $_GET['employeeId'];


PHP includes are really includes; they attach piece of code from that location to this location.

PaulPRO’s answer most likely tells what you want to achieve, but also other variables would be usable in the included file. So if you define $foo = "bar"; in index.php and include layout.php after that line, you could call this $foo variable in that layout.php file.

If you seriously want to pass an URL to the file, you can insert the URL into variable and in included file parse it with parse_url

EDIT: and yes, I do not suggest adding query params to actual include url as it starts to mess up with GET array and eventually you’ll just be confused what there should be and start smacking your head into wall.


Your include file will have access to the GET variables directly.


You can pass the value through Session



In person_info.php, start the session and put all session value in the variable and unset the session.


You can use $GLOBALS to solve this issue as well.

 $tit = "Hello";

 $GLOBALS["docTitle"] =  $tit;

 include ("my.php");


This is not so clean but this will work if you need to enter a static parameter:

if (true){
    echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=red%20bull' />";
    echo "<meta http-equiv='refresh' content='0;url=query_source.php?random=blue%20bull' />";

Just put it within your source code for the page which calls the query_source.php.

Once again, it is NOT very clean…