(no subject)
Brought to you by:
iridium
From: Ray v. B. <r_v...@ho...> - 2002-02-04 20:52:54
|
>(By the way: please subscribe to the list - postings from >non-subscribed addresses are held for approval until I either approve >them or rejects them.) > I did yesterday, hopefully, things go better now.. >>function extract($array) { >> >> while (list($key, $val) = each($array)) { >> $GLOBALS["\"". $key. "\""] = $val; >> } >>} > >Why have you wrapped $key in a pair of double quotes? Sorry, my fault! This was one of my experiments to see why the $GLOBALS construction was not working with me. At first, I did try your original function, without any result though.... > >>I tracked the data from the METAR string to the extract function. I >>found that at a given iteration of the loop, the temp_c key is >>coming by. Then at the end of the function, I checked for a variable >>$temp_c but this results in nothing to be printed with: >> >>printf($temp_c); >> >>Is the $GLOBAL contruction >>correct for php3.0.3? > >Well, I don't know? I was hoping that you could test that for me :-) >Try making a simple file like this: > >function f($a) { > $GLOBALS['foo'] = $a; >} > >f('Hello'); > >echo $foo; > >That should print Hello, indicating that the function f is able to >access the global variables through $GLOBALS. > Yes, that works fine with me... I know this must work in php, but on the other hand, why does the construction not work in the phpweather script I have. Then I tried following, I changed your script to this: <html> <head> <title>PHP Weather</title> </head> <body> <?php function f($a) { $GLOBALS['foo'] = $a; printf("GLOBALS[] within function: ".$GLOBALS['foo']."<br>"); printf("foo within function : ".$foo."<br>"); } function g() { printf("GLOBALS[] within other function: ".$GLOBALS['foo']."<br>"); printf("foo within other function : ".$foo."<br>"); } f('Hello'); printf("GLOBALS[] outside function: ".$GLOBALS['foo']."<br>"); printf("foo outside function : ".$foo."<br>"); g(); ?> </body> </html> and this is what I got: GLOBALS[] within function: Hello foo within function : GLOBALS[] outside function: Hello foo outside function : Hello GLOBALS[] within other function: Hello foo within other function : Apperently (in php3.0.3 only?) the variables created via $GLOBALS are only available in 'global' space and not in the function where it is added to the globals. Even in another function it is not available. Now that explains why I am not able to see a variable like temp_c in the phpweather script. It is added to the globals in a function, and it is used in another. This is what I got in the script: "16880919 minutes ago, at 00:00 UTC, the wind was calm in Aalborg, Denmark. The temperature was degrees Celsius ( degrees Fahrenheit), and the pressure was hPa ( inHg). The relative humidity was %. The sky was clear." I bet that all variables that are being used in this part of the script don't exist (or cannot be seen) in the function that prints this out. Checking on the php docu:(http://www.php.net/manual/en/language.variables.scope.php) "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." The only thing we have to do is making the $GLOBALS[$key] variables that are created in the extract() function available in the other functions. How can we do that (I'm a newbie to php)? >>By the way: I know that it would be best to ask the admin of the >>host provider to upgrade, I found out that my account is stored on >>some virtual server in a server farm. The company I obtained the >>account from only rents a virtual server, so I don't think that this >>company is able to upgrade php 3... unfortunately. > >But... it's really easy to install PHP4 - if they managed to install >PHP3, then it shouldn't take them more that half an hour to install >PHP4... but on the other hand: I would like to support all possible >versions of PHP, so it's not all bad :-) Yep, I'm going to ask them to upgrade to php4 , lets see what they have to say.... Thanks again for your help! Ray _________________________________________________________________ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx |