Variable scope problem?

This is a discussion on Variable scope problem? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I have a page search.php which uses Top.php and Bottom.php to do most webpage stuff common to ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-19-2006
J
 
Posts: n/a
Default 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...expected = or something.


****************TOP.PHP
<?
require_once("KTConstants.php");
require_once("PatientResults.php");
require_once("BuildForm.php");
require_once("Bottom.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><title>Search Page</title></head><body>';

echo "<table bgcolor='$KTPTTopColor' width = 800>";
echo '<tr><td>';
echo '<a href=search.php>Find</a>';
echo '<a href=add.php>Add</a>';
echo '</td></tr>';

}
?>


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


The problem is that the above
echo bgcolor='$KTPTTopColor'
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


Reply With Quote
  #2 (permalink)  
Old 01-19-2006
Colin McKinnon
 
Posts: n/a
Default Re: Variable scope problem?

"J" <notanymore> wrote:

> I tried using $GLOBALS['KTPTBlue']; perhaps I didn't do it right.
> The global keyword gives me an error...expected = or something.
>


guess so I would have expected it to.

>
> ****************TOP.PHP
> <?
> require_once("KTConstants.php");
> require_once("PatientResults.php");
> require_once("BuildForm.php");
> require_once("Bottom.php");
>


required/included files are added in the global scope

> function Top($redorblue)
> {
> //if ( $redorblue == 'red')


I guess you don't really mean to comment this line only - cos that would be
a syntax error

> $KTPTTopColor=$KTPTRed;
> else
> $KTPTTopColor=$KTPTBlue;
>


These variables are all out of scope.
Should be:
$KTPTTopColor=$_GLOBALS['KTPTRed'];

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


Yes - its out of scope.

C.
Reply With Quote
  #3 (permalink)  
Old 01-24-2006
J
 
Posts: n/a
Default Re: Variable scope problem?

Thanks for the tips.

I was told to "global"ize my vars I use in functions at the beginning of the
function just after the first '{'

you know, as stated plainly in the manual?

RTFM rings true again - but I won't learn. ;)

-JB


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:48 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0