RE: [PHP] disappearing php variables?

From: Date: Tue, 20 May 2003 20:14:29 +0000
Subject: RE: [PHP] disappearing php variables?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-148255@lists.php.net to get a copy of this message
Hi, I was looking at the code that you have given in your mail. The line <body> <?=getcwd();?> suggests that most probably your echo statements echo "rootPath is ".$rootPath."<br>"; echo "page is ".$page."<br>"; echo "pageTitle is ".$pageTitle."<br>"; are a part of this getcwd(); function. Global variables cannot be recognized inside functions, until especially declared so. The following may be of some help to you: The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example: $a = 1;[PARA]include "b.inc";[PARA] Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example: [PARA]$a = 1; /* global scope */ [PARA][PARA]Function Test () { [PARA] echo $a; /* reference to local scope variable */ [PARA]} [PARA][PARA]Test ();[PARA] This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function. An example: $a = 1;[PARA]$b = 2;[PARA][PARA]Function Sum () {[PARA] global $a, $b;[PARA][PARA] $b = $a + $b;[PARA]} [PARA][PARA]Sum ();[PARA]echo $b;[PARA] The above script will output "3". By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function. A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as: $a = 1;[PARA]$b = 2;[PARA][PARA]Function Sum () {[PARA] $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];[PARA]} [PARA][PARA]Sum ();[PARA]echo $b;[PARA] The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Hope this helps you. Regards, Mohit Mittal Software Engineer Patni Computer Systems (P) Ltd. Noida, Office Tel: 91 120 2516880 - 83 Ext: 1261 Residence Tel: 91 11 26890175 -----Original Message----- From: Megalion [mailto:php@megandodson.com] Sent: Tuesday, May 20, 2003 12:27 AM To: php-general@lists.php.net Subject: [PHP] disappearing php variables? I have a problem with php variables that are set outside an include, being empty when called inside the include. in http://ponder.megalion.org/index_photoskin.html I have the following code (snipped for length) $mtBlogURL = "http://ponder.megalion.org/"; $rootPath = "/usr/local/psa/home/vhosts/ponder.megalion.org/httpdocs"; if ($skin=="") { $skin = "venturaSunset"; } if ($page=="index") { $skinPath = "/mt/skins/"; } else if ( $page=="archive") { $skinPath = "/mt/skins/"; } else if ( $page=="category") { $skinPath = "/mt/skins/"; } else if ( $page=="date") { $skinPath = "/mt/skins/"; } else if ( $page=="individual") { $skinPath = "/mt/skins/"; } else { $skinPath = "/mt/skins/"; } include $rootPath."/mt/tmpl/php_functions.php"; include $mtBlogURL.$skinPath.$skin."/header.php"; in /mt/skins/venturaSunset/header.php, I have the following code: echo "rootPath is ".$rootPath."<br>"; echo "page is ".$page."<br>"; echo "pageTitle is ".$pageTitle."<br>"; ?> <html> <head> <title><?=$blogTitle?><?if ($pageTitle!="") { ?> - <?=$pageTitle?><?}?></title> <link rel="stylesheet" type="text/css" href="<?=$mtBlogURL?><?=$skinPath?><?=$skin?>/theme.css"> <SCRIPT src="<?=$skinPath?>rollover.js" TYPE="text/javascript"></SCRIPT> </head> <body> <?=getcwd();?> The output at the top of the page in the browser is as follows: rootPath is page is pageTitle is /usr/local/psa/home/vhosts/ponder.megalion.org/httpdocs/mt/skins/venturaSuns et source code: rootPath is <br>page is <br>pageTitle is <br><html> <head> <title></title> <link rel="stylesheet" type="text/css" href="/theme.css"> <SCRIPT src="rollover.js" TYPE="text/javascript"></SCRIPT> </head> I've been going over this for over 2 hours and I can't find any reason why the header.php thinks all my variables are empty. I even did a test where I created http://ponder.megalion.org/test.php that had <? $test = "This is a test"; include "include.php"; ?> and created also http://ponder.megalion.org/include.php that simply has <?=$test?> and it works. The include.php file is able to see php variables set in the calling file. So why is header.php unable to see variables set in index_photoskin.html? Btw I have php processing enabled for html files via the apache config file but I don't see that making a difference as I copied test.php as test.html and http://ponder.megalion.org/test.html works just as well as the other. I'm racking my brain as to what's going on... as some of you may recognize... this is my installation of the Movable Type blog software and I'm attempting to make it skinnable. Thanks Megalion

« previous php.general (#148255) next »