includes - vars not defined

Discussion in 'PHP' started by lektrikpuke, Aug 20, 2009.

  1. #1
    Hi y'all,

    I'm making a page with a lot of php includes. Everything works fine, but when I look at the server error log I'm getting a lot of messages complaining that variables are not defined. Again, everything's working, so the variables are defined, just not in the php includes. I can get around it by using if(isset(variable)), but I was just wondering what would cause this? Do I need to declare the variables as global in order to make the server happy if I'm using them in php includes?

    Thanks for helping me to understand. :D

    Rob
     
    lektrikpuke, Aug 20, 2009 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    The variables that are being used already are declared correct? As in maybe its the order of the includes you are having an issue wiht?

    include file with vars
    include file that uses vars

    and not the other way around.
     
    ccoonen, Aug 20, 2009 IP
  3. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #3
    The variables are declared in the original / host file / calling file that is using the includes.
     
    lektrikpuke, Aug 20, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, obviously they're not all declared otherwise you wouldn't get those errors. Everything may still appear to be working though depending on what the value of those variables is supposed to be. If it's just supposed to be $bla = false; then everything will still work because an undefined variable will also match this if ($bla) {}
     
    premiumscripts, Aug 20, 2009 IP
  5. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are you using the includes inside a function? If so, you need to use $GLOBALS
     
    Kyosys, Aug 21, 2009 IP
  6. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #6
    $query = "SELECT * FROM tbl WHERE code like '" . $code . "'";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);
    $var_name = $row[1]; // $var_name declared

    include called
    inside php include:

    echo $var_name; // gives error 'undefined variable' even though $var_name echoes to the page properly

    This is just one example. There is html outside of the php in the calling file and the include, and there are several includes on the page, some of which may use the variable before this include. Is that the problem? :eek:
     
    lektrikpuke, Aug 21, 2009 IP
  7. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You are not showing all the code:

    
    include called
    inside php include:
    
    Code (markup):
    Do not show us "concept code", show the real deal.
     
    premiumscripts, Aug 21, 2009 IP
  8. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #8
    That's not concept code. That is the actual code minus a couple of variables pulled from the array, and that is the actual code in the include. Also, I changed the variable name, but still, all in all, that's it. :)
     
    lektrikpuke, Aug 21, 2009 IP
  9. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #9
    If just about everything I have is an include, and the variable is used by a prior include (but not modified), can that cause this problem? :eek:
     
    lektrikpuke, Aug 21, 2009 IP