Variables and includes.

Discussion in 'PHP' started by dkin69, Apr 26, 2007.

  1. #1
    This should be a simple question.

    I have this code

    $target = 'blah';
    include 'include.php';

    Now, is it possible to have include.php communicate with $target, if so how???

    Thanks
     
    dkin69, Apr 26, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In your include.php file, you can use the $target variable as if it were defined in the include file itself.
    # Main file
    $target = 'blah';
    include 'include.php';
    
    # Include file (include.php)
    echo $target; // outputs "blah"
    PHP:
     
    rodney88, Apr 26, 2007 IP
  3. dkin69

    dkin69 Active Member

    Messages:
    644
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #3
    See thats what I thought, but its not working, this is my exact code, cut and paste.

    Include.php

    $lin = mysql_query("SELECT * FROM link order by id asc", $link) or die ("query 1: " . mysql_error());
    echo '<li><h2>LinkWork</h2>'
    . '<ul>';
    while ($li = mysql_fetch_array($lin)) {
    if ($current == $li['base'])
    {
    echo '';
    }
    else
    {
    echo '<li><a href="'.$li['web_url'].'" alt="'.$li['alt_tag'].'">'.$li['display'].'</a></li>';

    }
    }

    Including file

    <?php
    $current = 'bluewidget';
    include("include.php"); ?>


    This is on a wordpress blog if that helps.

    Thanks
     
    dkin69, Apr 26, 2007 IP
  4. dkin69

    dkin69 Active Member

    Messages:
    644
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    ok, I tested it

    I inserted this into my page
    $numfive = 5;

    I inserted this into my include

    $numfour = 4;
    $done = $numfour + $numfive;

    when the page is called it returns "4"

    any ideas???
     
    dkin69, Apr 26, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Why complicate things? When you debug, use the lowest forms.

    Echo $numfive in the included page and see if it returns anything.

    Peace,
     
    Barti1987, Apr 26, 2007 IP