RE: [PHP] disappearing php variables?

From: Date: Tue, 20 May 2003 08:13:35 +0000
Subject: RE: [PHP] disappearing php variables?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-148262@lists.php.net to get a copy of this message
Hi Thanks for your reply... getcwd() isn't a user-defined function in my case. I was using the php function getcwd() get current working directory as a debug tool to ascertain where "I" was at that point in the script... clearly the current working directory had changed from /usr/local/psa/home/vhosts/ponder.megalion.org/httpdocs/ which is where it should be at the start of the script as it's being called by index_photoskin.html located in that directory and ends up in /usr/local/psa/home/vhosts/ponder.megalion.org/httpdocs/mt/skins/venturaSunset right after the include of the header.php file as the line you were referring to "echo getcwd()" is located inside it. Thanks for confirming my expectations on what should be happening... the variables are not defined within a function and should therefore be available to header.php (the include) but are not for some strange reason. I tried changing the line inside the header.php that was trying to output the $rootPath variable defined in the index_photoskin.html file from : echo "rootPath is ".$rootPath."<br>"; to echo "rootPath is ".$GLOBALS["rootPath"]."<br>"; and still no luck. I still get "rootPath is " Stranger and stranger that my entire $GLOBAL array has virtually disappeared in the eyes of the header.php include. Any more ideas? Megalion At 01:14 PM 5/20/2003 -0700, you wrote:
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>"; ?> <<<< <?=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/venturaSun s et source code: rootPath is page is pageTitle is 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#148262) next »