variable scope problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J

    variable scope problem?

    I have a page search.php which uses Top.php and Bottom.php to do most
    webpage stuff common to all pages. Top.php includes/requires
    KTConstants.php .
    Top.php can't see Constants/variables like $favoriateColor ='234566'; defined
    in KTConstants.php
    I tried include instead of require.
    I tried using $GLOBALS['KTPTBlue']; perhaps I didn't do it right.
    The global keyword gives me an error...expecte d = or something.


    *************** *TOP.PHP
    <?
    require_once("K TConstants.php" );
    require_once("P atientResults.p hp");
    require_once("B uildForm.php");
    require_once("B ottom.php");

    function Top($redorblue)
    {
    //if ( $redorblue == 'red')
    $KTPTTopColor=$ KTPTRed;
    else
    $KTPTTopColor=$ KTPTBlue;

    echo $KTPTBlue; //Doesn't work
    echo $KTPTTopColor; //Doesn't work

    echo '<html><head><t itle>Search Page</title></head><body>';

    echo "<table bgcolor='$KTPTT opColor' width = 800>";
    echo '<tr><td>';
    echo '<a href=search.php >Find</a>';
    echo '<a href=add.php>Ad d</a>';
    echo '</td></tr>';

    }
    ?>


    *************** KTConstants.php
    <?
    $KTPTRed = 'FF9999';
    $GLOBALS['KTPTBlue'] = '6666FF'; //tried both
    $KTPTServer='';
    $KTPTDatabase=' ';
    $KTPT='';
    ?>


    The problem is that the above
    echo bgcolor='$KTPTT opColor'
    always comes out blank on view source.

    Even when I insert into top.php.
    echo $KTPTBlue; nothing happens.
    Although it works when I echo it in KTConstants.php .

    Thanks in advance,
    -JB


  • Oli Filth

    #2
    Re: variable scope problem?

    J said the following on 19/01/2006 21:58:[color=blue]
    > I have a page search.php which uses Top.php and Bottom.php to do most
    > webpage stuff common to all pages. Top.php includes/requires
    > KTConstants.php .
    > Top.php can't see Constants/variables like $favoriateColor ='234566'; defined
    > in KTConstants.php
    > I tried include instead of require.
    > I tried using $GLOBALS['KTPTBlue']; perhaps I didn't do it right.
    > The global keyword gives me an error...expecte d = or something.
    >
    >[/color]
    <SNIP CODE>

    See Example 12-2 at
    http://www.php.net/manual/en/languag...bles.scope.php. Note where
    the "global" keyword is used.

    --
    Oli

    Comment

    • Al

      #3
      Re: variable scope problem?

      > See Example 12-2 at[color=blue]
      > http://www.php.net/manual/en/languag...bles.scope.php. Note where
      > the "global" keyword is used.[/color]

      Or use the $GLOBAL["global"] thing in your function rather than the
      contants file.

      Comment

      Working...