PDA

View Full Version : How do I pass a variable to an include in PHP?



tc101
July 31st, 2008, 03:17 AM
How do I pass a variable to an include in PHP? For example in the two simple files below, how would I pass a variable from test.php to include.php? I know how to do this with a form using $_POST, but I can't figure out how to do it in an include.

test.php

<html>
<body>
<b>first html</b>
<p>
<?php
include 'include01.php';
?>
<p>
<b>last html</b>
</body>
</html>

include.php

<?php
echo "this comes from the include";
?>

theY4Kman
July 31st, 2008, 03:58 AM
Just define the variables you want to send before including the new file. Once included, the new file will be able to access those variables. For example:

<?php
$var = "Hello, World!";
include("included.php");
?>

<?php
// included.php
echo $var;
?>That will output:

Hello, World!