...

View Full Version : PHP Includes with Variables?



JAG
05-24-2011, 06:25 PM
Hey guys, I'm a PHP beginner and was wondering if it is possible to send variables with includes?

For example, if I wanted to do an include from my faq.php page, is it possible to do something like this:

<?php include 'includes/global_footer.php', 'FAQ'; ?>

and from my about.php page:

<?php include 'includes/global_footer.php', 'About'; ?>

Where global_footer.php contains:

<?php

// Using pseudo PHP code

if page == FAQ
<div>This is my footer for the FAQ page</div>

if page == About
<div>This is my footer for the About page</div>

else
<div>This is my footer for all the other pages</div>

?>

How would I write this using PHP code?

DanInMa
05-24-2011, 06:37 PM
I am learning about php myself. I'm not sure if what you pose is possible, but I do something similar with my header div like so :

right after the <body> tag I do my include:
<?php include 'inc/header.php' ?>


<?php
$page = basename($_SERVER['PHP_SELF']);
echo"<div id=\"header\">";
echo"<div class=\"dansplace\">Dan's Place</div>";
echo"<ul class=\"headerUL\">";

if ($page=="index.php")
{

echo"<li class=\"current\"><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="Projects.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li class=\"current\"><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="blog.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li class=\"current\"><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="about.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li class=\"current\"><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="contact.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li class=\"current\"><a href=\"/contact.php\">Contact</a></li>";
}
else
{
echo"<li class=\"current\"><a href=\"/index.php\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
echo" </ul>";
echo"</div>";
?>

Fou-Lu
05-24-2011, 06:50 PM
No you cannot send arguments to a local included script.
I see no reason to do so though, if you need dynamic input provided that value into the superglobal before including it in. Alternatively, you can remotely access it using an http wrapper by providing the http://site.com/script.php instead, but this will parse it as its served. You can provide a querystring with this, but you can only capture the results, not deal directly with any developed variables. That will also only work if you have allow_url_include available.

angst
05-24-2011, 06:53 PM
Hey guys, I'm a PHP beginner and was wondering if it is possible to send variables with includes?

For example, if I wanted to do an include from my faq.php page, is it possible to do something like this:

<?php include 'includes/global_footer.php', 'FAQ'; ?>

and from my about.php page:

<?php include 'includes/global_footer.php', 'About'; ?>

Where global_footer.php contains:

<?php

// Using pseudo PHP code

if page == FAQ
<div>This is my footer for the FAQ page</div>

if page == About
<div>This is my footer for the About page</div>

else
<div>This is my footer for all the other pages</div>

?>

How would I write this using PHP code?


no this is not possible. but you could include the page/section name in the include like.


SomeInclude.php:


$PageTitle = "About";



then in your script, simply call the variable after the include, like;



include "SomeInclude.php";
echo $PageTitle;

tangoforce
05-24-2011, 07:30 PM
You might like to see my answer to a similar question here (http://www.codingforums.com/showpost.php?p=1093720&postcount=2).

JAG
05-24-2011, 11:05 PM
I am learning about php myself. I'm not sure if what you pose is possible, but I do something similar with my header div like so :

right after the <body> tag I do my include:
<?php include 'inc/header.php' ?>


<?php
$page = basename($_SERVER['PHP_SELF']);
echo"<div id=\"header\">";
echo"<div class=\"dansplace\">Dan's Place</div>";
echo"<ul class=\"headerUL\">";

if ($page=="index.php")
{

echo"<li class=\"current\"><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="Projects.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li class=\"current\"><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="blog.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li class=\"current\"><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="about.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li class=\"current\"><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
elseif ($page=="contact.php")
{
echo"<li><a href=\"/\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li class=\"current\"><a href=\"/contact.php\">Contact</a></li>";
}
else
{
echo"<li class=\"current\"><a href=\"/index.php\">Home</a></li>";
echo"<li><a href=\"/Projects.php\">Projects</a></li>";
echo"<li><a href=\"/wp/\">Blog</a></li>";
echo"<li><a href=\"/about.php\">About Me</a></li>";
echo"<li><a href=\"/contact.php\">Contact</a></li>";
}
echo" </ul>";
echo"</div>";
?>

This is EXACTLY what I was looking for.

Many thanks, DanInMa. You are my hero. :thumbsup:

Inigoesdr
05-25-2011, 12:02 AM
Don't forget that your variables are accessible in the scope of the new file, so while you can't "pass" the variable to the new file, it will be available in the local scope.

$selectedPage = 'FAQ';

include 'otherpage.php'; // $selectedPage will be available in this file for your if() or switch()



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum