NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
superglobals and php


Joined: 13 Feb 2007
Posts: 4
Reply with quote
Hi,

I am having trouble running the debugger on my php files that include global variables. It doesn't seem able to continue beyond the point at which these are called, even though the specified variable (in another file) is correctly included in the current file. When I run the code on the server itself it runs without a problem. Is there something I'm doing wrong here?

For example, I have one include file, which contains my database details: for example..

Code:

global $db_server;
$db_server = "localhost";


Then, in the main php file, I have a function, within which, I would refer to the above global variable as follows:

Code:

$result = mysql_query("SELECT * from `$GLOBALS[db_server]` WHERE pet = 'dog'");


The debugger fails to correctly recognise the reference to the global variable above. Any ideas?

Thanks a lot for your help,

Rich
View user's profileFind all posts by underiSend private message


Joined: 08 Feb 2007
Posts: 67
Reply with quote
Don't you have to put "global $db_server;" within the function? E.g.

Include file:
Code:
$db_server = "localhost";

Main file:
Code:
function test()
{
    global $db_server;
    $result = mysql_query("SELECT * from `$GLOBALS[db_server]` WHERE pet = 'dog'");
}


You can see more info here.
View user's profileFind all posts by nothsaSend private message


Joined: 13 Feb 2007
Posts: 4
Reply with quote
Thanks for the link.

I'm sorry I wasn't clear about my top example. The reason I used the global $variable; in the top file, was because it was also within a function.

I've had a closer look, and it seems that the issue is the debugger not being able to handle the use of $GLOBAL['variable_name'] within quotes. For example:

PHP5 (and PHP4 for that matter) correctly recognise the following:

Code:
echo "Good morning. It is $GLOBALS[time_of_day] <br />";

While, the debugger must have the global variable presented in the following fashion (outside of the quotes):
Code:
echo "Good morning. It is" . $GLOBALS['time_of_day'] . " <br />";


Is there a setting I can change to allow the debugger to handle global variables in the manner of the first example?

Thanks,

Rich
View user's profileFind all posts by underiSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
Dear Rich,
Debugger does never recognize any variables whatever... it's just not its job. Please consider debugger as a mirror. It reflects what php engine is doing. If PHP does not understand your code, debugger will show you so.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Veteran

Joined: 26 Dec 2006
Posts: 253
Location: Phoenix, AZ
Reply with quote
underi wrote:
PHP5 (and PHP4 for that matter) correctly recognise the following:

Code:
echo "Good morning. It is $GLOBALS[time_of_day] <br />";


You need to at least put the array index (time_of_day) into single quotes there. It'll work without them most of the time, but not always, and it is technically an error to skip them. The only reason it works at all is because of an implementation detail in PHP.

Quote:
While, the debugger must have the global variable presented in the following fashion (outside of the quotes):
Code:
echo "Good morning. It is" . $GLOBALS['time_of_day'] . " <br />";


I long ago gave up trying to directly include anything but simple variable references in strings without bracketing them; I just found too often that, even when it did work, I'd later need to make a change that broke it. So now I just always put braces around the reference:

Code:
echo "Good morning. It is {$GLOBALS['time_of_day']} <br />";


Good luck.
View user's profileFind all posts by bobwilliamsSend private messageVisit poster's website


Joined: 13 Feb 2007
Posts: 4
Reply with quote
Thanks for your responses gents. and thanks, Bob, for the clarification.
View user's profileFind all posts by underiSend private message
superglobals and php
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic