1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Variable Scope With Include Files

Discussion in 'PHP' started by sayyes, Dec 2, 2005.

  1. #1
    I'm just learning PHP, and I realize this is a very basic question, but some of the things I've found on the Internet aren't working!

    I want to be able to set a unique page title for every page at my site. So, I thought I could set a php variable at the top of my page and then include my header.php file which uses that variable. So, at the top of every page I have:

    <?php
    $title = "Unique Title";
    include("http://www.manageyourdollars.com/common/header.php");
    ?>

    And in header.php I have (among other things):

    <TITLE><?php echo $title; ?></TITLE>

    I've found a couple tutorials on the web that say this should work, unless I'm misreading them.

    What am I missing?
     
    sayyes, Dec 2, 2005 IP
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Include requires local file paths, not http: URLs. So it would be more like

    include("/path/to/your/local/files/header.php");
     
    TheHoff, Dec 2, 2005 IP
    sayyes likes this.
  3. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can only include files on a local system (even then you can have loads of fun with the open_basedir error)

    any variable in the included file is accesible

    it will be a huge security flaw if php files could be opened across sites

    ok lets say heres the file u want to include (call it common.php and its in sam folder as rest of your site)
    
    <?php
    
    $commonTitle = 'title goes here';
    
    ?>
    
    PHP:
    and heres every page of your site

    
    <?php
    
    include( 'common.php' );
    
    
    print '<html><head><title>'.$commonTitle.'</title> and so on and so forth';
    
    ?>
    
    PHP:
     
    cornelius, Dec 2, 2005 IP
  4. bvcarter

    bvcarter Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Variables that are defined in included files are accessible everywhere else, but are files that are defined somewhere else accessible in the included file?
     
    bvcarter, Aug 31, 2006 IP
  5. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes. Think of it as copying and pasting the code from the include file into the "parent" file. Once php parses the includes it's all one big happy file :)
     
    void, Aug 31, 2006 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just note too, though, that once you're trying to access that variable from within a function, you need to add the line:
    global $variable_name;

    to the function: by default, a function cannot access a variable declared outside of that function.
     
    TwistMyArm, Aug 31, 2006 IP
  7. surefire

    surefire Guest

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    For the whole story, I can't recommend highly enough the php.net site:

    http://us2.php.net/manual/en/function.include.php

    Some interesting quotes from that page:

    And

     
    surefire, Sep 1, 2006 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    On the point of including via HTTP, please know this up front: if you include http://www.example.com/somefile.php, you are including the output of that script, not the file itself.

    That is to say that if you visit http://www.example.com/somefile.php with your browser, what you see in that browser has to be valid PHP otherwise including it via HTTP will not work. It doesn't magically get the code itself, if you get my drift...
     
    TwistMyArm, Sep 1, 2006 IP
  9. surefire

    surefire Guest

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That's a great point.
     
    surefire, Sep 1, 2006 IP
  10. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Why does everyone insist on using parenthesis with include? It's not a function, so it doesn't need parenthesis.

    A good idea, along with what you are doing, is to have the headers or other file check if each of those variables are set (isset()) and, if they aren't, provide a default so you don't accidently end up with blank titles (or keywords, or whatever else you use variables across scripts for).
     
    Gordaen, Sep 1, 2006 IP
  11. rajeev_seo

    rajeev_seo Peon

    Messages:
    211
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yes. Think of it as copying and pasting the code from the include file into the "parent" file. Once php parses the includes it's all one big happy file
     
    rajeev_seo, Apr 24, 2011 IP