passing var to include

Discussion in 'PHP' started by lektrikpuke, May 17, 2008.

  1. #1
    Hi y'all,

    I'm having a problem passing a variable to an include. I realize it's a scope issue (as it's technically a different page), just don't know what to do about it. :confused:

    origin_page.php (contents)
    $var=whatever;

    if(condition){
    include 'another_php_page.php';
    }

    *****************
    another_php_page.php (contents)
    echo $var; // nothing here
     
    lektrikpuke, May 17, 2008 IP
  2. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #2
    make $var a constant and it shoudl work

    bit heavy handed but it should work

    could also try messing around with global
     
    lanmonkey, May 17, 2008 IP
  3. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Make sure you aren't accidentally redeclaring $var. That's usually the issue.
     
    Altari, May 17, 2008 IP
  4. coffear

    coffear Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #4
    this is not a scope issue. When you include a file as far as things are concerned any code within the included file will act as if it were in the original file.

    The most likely answer is as Altari has suggested which is that you have redeclared $var or possibly never declared it in the 1st place.
     
    coffear, May 17, 2008 IP
  5. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #5
    No, including file doesn't change the scope. Function/class declaration does.

    1.You can access variable in included function if it is global.

    To declare global variable declare it not inside the function.
    Inside function you can use
    $GLOBALS['var'] = 'value';
    PHP:
    To access global var you can use
    
    global $var;
    echo $var;
    
    PHP:
    or
    echo $GLOBALS['var']
    PHP:
    2. If you need bulk variables passing, you can pass them to a functions using compact() and extract().
     
    wmtips, May 17, 2008 IP
    szalinski likes this.
  6. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #6
    Thanks for all the help, but I still am not seeing results. More help?

    if(isset($_POST['rating_val'])){
    $rating_val=$_POST['rating_val'];
    $mess = A rating of: " . $rating_val . " " . $trim_url; // $trim_url declared in page that this page is included in
    echo "This is trim_url: &nbsp;" . $trim_url . "<br />"; // Not showing up so not showing in message either
    $to = "address@domain.com";
    $subject = "Rating";
    $body = $mess;
    if (mail($to, $subject, $body)){
    echo "<p>Thank you.</p>";
    } else {
    echo "<p>There was an error.</p>";
    }
     
    lektrikpuke, May 17, 2008 IP
  7. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Are you sure your condition is being met and the file is actually being included?
     
    LogicFlux, May 17, 2008 IP
  8. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #8
    I'm pretty sure, as the mail gets sent with everything but the one variable. Just in case this makes a difference, the variable is derived from stripping the original url (when the page is first loaded). Then if (condition) include other page (which is a form for mailing info). The info that is mailed is from a form and the variable that is derived from the stripped url.
     
    lektrikpuke, May 17, 2008 IP
  9. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #9
    Ok, figured it out (after I posted the last one). I added the var as a hidden field in the form. Then it was there in the post info. Sorry to have wasted everyone's time.
     
    lektrikpuke, May 17, 2008 IP