Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    61
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Question PHP Includes with Variables?

    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?

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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' ?>

    Code:
    <?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>";
    ?>

  • Users who have thanked DanInMa for this post:

    JAG (05-24-2011)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Quote Originally Posted by JAG View Post
    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:
    PHP Code:
    $PageTitle "About"

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

    PHP Code:
    include "SomeInclude.php";
    echo 
    $PageTitle

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    You might like to see my answer to a similar question here.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    61
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by DanInMa View Post
    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' ?>

    Code:
    <?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.

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    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.
    PHP Code:
    $selectedPage 'FAQ';

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

  • Users who have thanked Inigoesdr for this post:

    JAG (05-25-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •