Reply to Re: variable scope problem?

Your name:

Reply:


Posted by DXTrim on 02/14/06 18:16

Hi,

Variables outside a function are not available within the function unless
you declare
them global. Either if your server allows it, you can refer to those using
Globals, but attention:

$GLOBALS['KTPTBlue'] = '6666FF'; is nto correct. It should be: $_GLOBALS

If globals are turned off you can try by including your files, where the
variables are, inside the function, something like this:

function Top($redorblue){

require_once("KTConstants.php");
require_once("PatientResults.php");
require_once("BuildForm.php");
require_once("Bottom.php");

do_stuff

} // end function

Hope this helps,

DXTrim


"J" <notanymore> wrote in message
news:RYOdnfvay6RYlk3enZ2dnUVZ_tGdnZ2d@adelphia.com...
> version 5.1.2
>
> 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
>
>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация