PHP - Include a PHP File and Also Send Query Parameters

PHP - include a php file and also send query parameters

Imagine the include as what it is: A copy & paste of the contents of the included PHP file which will then be interpreted. There is no scope change at all, so you can still access $someVar in the included file directly (even though you might consider a class based structure where you pass $someVar as a parameter or refer to a few global variables).

How to pass arguments to an included file?

index.php:

<?php
$my_header = 'PHP - Include a PHP File and Also Send Query ParametersPHP - Include a PHP File and Also Send Query Parametersaaaa';
include 'header.php';
?>

and header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php echo $my_header ?> " />
<meta name="Description" content=" <?php echo $my_header ?> " />
<title> <?php echo $my_header ?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>

It's not an ideal solution, but I understand it's your first steps in php.

PS. Your Doctype doesn't match the code. I've adjusted your header html to be XHTML.

PHP include with a get variable

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['id'] is defined before you call:

include('/newsArticles.php');

Then newsArticles.php will also have access to $_GET['id'];

How to pass parameters with `include`?

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'];

Include Local PHP File and Pass Parameters

I'd like to avoid doing any needless HTTP requests on my site to improve performance

include also makes an HTTP request when you supply an HTTP URI to it, so instead of the client's asynchronous include (for example using AJAX or an iframe) you do it synchronously on the server, therefore making the page load even slower.

So while the include may work as intended (you want to include the output, right?), it will definitely not speed up your site.

the PHP I'm calling expects some parameters to be passed via HTTP GET. I'd like to keep it that way since I also want to be able to access those PHP files from other places.

Then alter those files and set the appropriate variables before including them. Or even better, refactor it into functions. For example, if the file you want to include looks like this:

$username = $_GET['username'];
print "User: $username";

Then refactor into a function and store in a separate file:

function PrintUsername($username)
{
print "User: $username";
}

And call it appropriately:

include('printusername.php');
PrintUsername($_GET['username']);

(You might want to throw in an isset() here and there.) Now you can alter your code that also needs this output:

include('printusername.php');
PrintUsername($someOtherVariable);

Now you don't have to rely on URL or $_GET or include magic, but simply use all functions as they're meant to.

Is there a way to use parameters in PHP include paths?

No. You can set a variable and use it in just like it was declared in the file though. Read up here: http://www.php.net/manual/en/function.include.php

<?php 
$format = "list";
include("list-options.php");
?>

include file passing a parameter

  1. Try add absolute path to your file with __DIR__ constant (http://php.net/manual/en/language.constants.predefined.php) something like this:

    include __DIR__."/bp/businessplan.php?id=$l";

it must solve your path issue.


  1. And if you want get content many times you can try use return instead ob_* functions. Please see my example below:

file test.php

<?php
$r = '';
for ($i=0;$i<10;$i++) {
$_GET['id'] = $i;
$r .= include 'businessplan.php';
}

echo $r;

file businessplan.php

<?php
return 'your html here'.$_GET['id'].PHP_EOL;

And when I run test.php it show me this lines:

your html here0
your html here1
your html here2
your html here3
your html here4
your html here5
your html here6
your html here7
your html here8
your html here9

I hope it help

EDIT 1


  1. file not found issues - try remove ?id=$l from file name and pass it to $_GET array as I suggest above.


Related Topics



Leave a reply



Submit