Connecting Tech Pros Worldwide Forums | Help | Site Map

how does one access global variables when register_globals is off?

lkrubner@geocities.com
Guest
 
Posts: n/a
#1: Jul 17 '05

If I set a variable at the top of my code like this:

$name = "Lawrence";

It is now a global variable.

If, later on, in a function, I want to do this:

function uppercaseName() {
global $name;
$name = ucfirst($name);
}



Will the global keyword work? If not, where do I find the $name
variable?


Micha³ Wo¼niak
Guest
 
Posts: n/a
#2: Jul 17 '05

re: how does one access global variables when register_globals is off?


One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable lkrubner@geocities.com's handwriting:
[color=blue]
>
> If I set a variable at the top of my code like this:
>
> $name = "Lawrence";
>
> It is now a global variable.
>
> If, later on, in a function, I want to do this:
>
> function uppercaseName() {
> global $name;
> $name = ucfirst($name);
> }
>
>
>
> Will the global keyword work? If not, where do I find the $name
> variable?[/color]

AFAIK, register_globals affects $_POST and $_GET vars *only* (if on, it
registers, say, $_POST['myvar'] as a global $myvar). You don't have to
worry about your globals.

Although it is a good habit to always refer to global vars as
$GLOBALS['varname'] (in your example: $GLOBALS['name']) as this gives
you a clear distinction between global and local vars; otherwise you
might get lost somewhere and get misbehaviour that you cannot find the
reason of.

Cheers
Mike
lkrubner@geocities.com
Guest
 
Posts: n/a
#3: Jul 17 '05

re: how does one access global variables when register_globals is off?


I usually follow your advice about using $GLOBALS['varname'], but my
code broke when I moved to a server with register+globals off, so I
assume some important variables never make it into GLOBALS when
register+globals is off.

Daniel C. Bastos
Guest
 
Posts: n/a
#4: Jul 17 '05

re: how does one access global variables when register_globals is off?


In article <d3l9st$t1j$1@213-238-64-25.adsl.inetia.pl>, Micha³ Wo¼niak wrote:[color=blue]
> One quick glance of an experienced eye allowed to understand the blurred
> and almost unreadable lkrubner@geocities.com's handwriting:[color=green]
>>
>> If I set a variable at the top of my code like this:
>>
>> $name = "Lawrence";
>>
>> It is now a global variable.
>>
>> If, later on, in a function, I want to do this:
>>
>> function uppercaseName() {
>> global $name;
>> $name = ucfirst($name);
>> }
>>
>> Will the global keyword work? If not, where do I find the $name
>> variable?[/color][/color]

It will work. In fact, this is the a proper way of doing this.
[color=blue]
>
> AFAIK, register_globals affects $_POST and $_GET vars *only* (if on, it
> registers, say, $_POST['myvar'] as a global $myvar). You don't have to
> worry about your globals.[/color]

It affects environment variables, cookies and session as well.

http://www.php.net/manual/en/security.globals.php
JDS
Guest
 
Posts: n/a
#5: Jul 17 '05

re: how does one access global variables when register_globals is off?


On Thu, 14 Apr 2005 01:18:13 -0700, lkrubner wrote:
[color=blue]
> If, later on, in a function, I want to do this:
>
> function uppercaseName() {
> global $name;
> $name = ucfirst($name);
> }[/color]

Honestly, you really never want to do that. I wish I could concisely
explain why, but suffice it to say that using global variables is bad
because:

* it breaks the scope concept of the programming language
* in complex programming projects (and even simple ones) you can clobber
variables and create a real mess
* i'm sure there are other reasons

This is a decent article explaining the problems with globals (in a C
programming context but the concepts apply to PHP):

http://c2.com/cgi/wiki?GlobalVariablesAreBad

Why do you think that register_globals is off by default?

Your function above should take a value as input and return the uppercased
value as output. That is how functions are designed to work. The way you
wrote the function is *very* poor programming practice and continuing to
do that will bite you later. It will. Really.

try this:

function uppercasename($name){
return ucfirst($name);
}

In any case, I suggest using defined constants for things that need to be
global in scope. (Not necessarily appropo for your example).

later...

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Daniel C. Bastos
Guest
 
Posts: n/a
#6: Jul 17 '05

re: how does one access global variables when register_globals is off?


In article <pan.2005.04.14.13.55.12.743161@example.invalid> , JDS wrote:[color=blue]
> On Thu, 14 Apr 2005 01:18:13 -0700, lkrubner wrote:
>[color=green]
>> If, later on, in a function, I want to do this:
>>
>> function uppercaseName() {
>> global $name;
>> $name = ucfirst($name);
>> }[/color]
>
> Honestly, you really never want to do that. I wish I could concisely
> explain why, but suffice it to say that using global variables is bad
> because:
>
> * it breaks the scope concept of the programming language
> * in complex programming projects (and even simple ones) you can clobber
> variables and create a real mess
> * i'm sure there are other reasons
>
> This is a decent article explaining the problems with globals (in a C
> programming context but the concepts apply to PHP):[/color]

Every problem requires thinking. You can't simply say it's bad to do
this or that. It's like the goto-hate-movement. Gotos, for example,
are still well used today by programmers who know well how to do it.

[...]
[color=blue]
> Why do you think that register_globals is off by default?[/color]

Because he probably checked the documentation and tested with a
default installation.

http://www.php.net/manual/en/security.globals.php
[color=blue]
> Your function above should take a value as input and return the uppercased
> value as output. That is how functions are designed to work. The way you
> wrote the function is *very* poor programming practice and continuing to
> do that will bite you later. It will. Really.
>
> try this:
>
> function uppercasename($name){
> return ucfirst($name);
> }
>[/color]

I agree with you here, but this is just one case.
[color=blue]
> In any case, I suggest using defined constants for things that need to be
> global in scope. (Not necessarily appropo for your example).[/color]

What if you want to change them?
JDS
Guest
 
Posts: n/a
#7: Jul 17 '05

re: how does one access global variables when register_globals is off?


On Thu, 14 Apr 2005 09:47:51 +0000, Daniel C. Bastos wrote:
[color=blue]
> I agree with you here, but this is just one case.
>[color=green]
>> In any case, I suggest using defined constants for things that need to be
>> global in scope. (Not necessarily appropo for your example).[/color]
>
> What if you want to change them?[/color]

Well, okay, so constants are not perfect for everything.

However, (and I am assuming a newbie level programming expertise on the
part of the OP), unless you really know what you are doing, it is bad to
get into the *habit* of using global variables.

I agree that it is not *always* bad to use globals. Any statement with
"always" or "never" in it is almost certainly false. However it is very
bad, IMO, to teach a new programmer to use globals to do everything.
Rules are meant to be broken, but they really shouldn't be broken until
one learns the rules in the first place, and can understand *why* breaking
a rule here or there would work, or *not* work.

I'm basing this on the OP's complete misuse of globals in and out of a
function (his uppercaseName() function example) and additional
misunderstanding of how functions are meant to work.

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Daniel C. Bastos
Guest
 
Posts: n/a
#8: Jul 17 '05

re: how does one access global variables when register_globals is off?


In article <pan.2005.04.14.16.29.55.756766@example.invalid> , JDS wrote:[color=blue]
> On Thu, 14 Apr 2005 09:47:51 +0000, Daniel C. Bastos wrote:
>[color=green]
>> I agree with you here, but this is just one case.
>>[color=darkred]
>>> In any case, I suggest using defined constants for things that need to be
>>> global in scope. (Not necessarily appropo for your example).[/color]
>>
>> What if you want to change them?[/color]
>
> Well, okay, so constants are not perfect for everything.
>
> However, (and I am assuming a newbie level programming expertise on the
> part of the OP), unless you really know what you are doing, it is bad to
> get into the *habit* of using global variables.[/color]

I disagree with your didacticism. I tell myself to go ahead and try my
own ideas and see if they work well. If they work well, then they work
well. I consider myself unexperienced in programming in general, but I
think I started progressing when I decided to drop all of these
``lessons''. I don't think level of knowledge matters, do you?

I think it's better to think for yourself. Someone tells you ``it's
better to write a function for this one specific case'', you should
think ``Why?'' and see if you can find reasons in favor and against
the statement. I think you truly learn this way.

[...]
[color=blue]
> I'm basing this on the OP's complete misuse of globals in and out of a
> function (his uppercaseName() function example) and additional
> misunderstanding of how functions are meant to work.[/color]

Your suggestion was good, IMO.
JDS
Guest
 
Posts: n/a
#9: Jul 17 '05

re: how does one access global variables when register_globals is off?


On Thu, 14 Apr 2005 12:03:08 +0000, Daniel C. Bastos wrote:
[color=blue]
> I disagree with your didacticism. I tell myself to go ahead and try my
> own ideas and see if they work well. If they work well, then they work
> well. I consider myself unexperienced in programming in general, but I
> think I started progressing when I decided to drop all of these
> ``lessons''. I don't think level of knowledge matters, do you?
>
> I think it's better to think for yourself. Someone tells you ``it's
> better to write a function for this one specific case'', you should
> think ``Why?'' and see if you can find reasons in favor and against
> the statement. I think you truly learn this way.
>
> [...][/color]

Think for yourself, yes. But there is no point in reinventing the wheel.

I'll take guitar playing as an example. There were several technique
related issues that I actually learned from guitar instructors. Aside
from that, I am completely self taught. The technique issues were
specific things that I would have taken a *very* long time to figure out
myself, and that greatly improved my own technique, and, given long enough
entrenchment, things that would have been very hard for me to un-learn
once I finally came to the realization that my poor, self-taught technique
was the bottleneck preventing me from improving further. (actually, my
self taught technique *is* a bottleneck, still).

In the case of programming, I was being didactic about *technique* issues.
Not *style* issues. There are many tried and true reasons why globals are
not usually the best solution for a problem -- and it often does pay to
learn from people who have tread there in the past.

I was not suggesting a style or preference as better than another. I
wouldn't do that (well, I try not to), and I think that if you learn
enough of the "why" behind my blanket "don't do that" statements against
globals, you would see the difference. Maybe you do.

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Micha³ Wo¼niak
Guest
 
Posts: n/a
#10: Jul 17 '05

re: how does one access global variables when register_globals is off?


I think we are getting OT here. The question was "should it work" and the
answer is "yes", unless of course $name is actually a... registered
global. :)
I think a good idea would be to show how the $name was originally
declared. And changing $name to $GLOBALS['name'] everywhere in the code.
If such a change will break the code => $name is most probably a
registered global (e.g. from POST or GET) => it will NOT work with
register_globals off, and it is a great example why shoul it be always
of and why shoul one use $GLOBALS['somevar'], $_POST['someothervar'] and
so on instead of $somevar and $someothervar.

Cheers
Mike
Daniel C. Bastos
Guest
 
Posts: n/a
#11: Jul 17 '05

re: how does one access global variables when register_globals is off?


In article <pan.2005.04.14.19.44.45.538648@example.invalid> , JDS wrote:[color=blue]
> On Thu, 14 Apr 2005 12:03:08 +0000, Daniel C. Bastos wrote:
>[color=green]
>> I disagree with your didacticism. I tell myself to go ahead and try my
>> own ideas and see if they work well. If they work well, then they work
>> well. I consider myself unexperienced in programming in general, but I
>> think I started progressing when I decided to drop all of these
>> ``lessons''. I don't think level of knowledge matters, do you?
>>
>> I think it's better to think for yourself. Someone tells you ``it's
>> better to write a function for this one specific case'', you should
>> think ``Why?'' and see if you can find reasons in favor and against
>> the statement. I think you truly learn this way.
>>
>> [...][/color]
>
> Think for yourself, yes. But there is no point in reinventing the wheel.[/color]

I probably agree with you here. I say ``probablly'' because
reinventing the wheel is a bit necessary sometimes for learning. But
``reinventing the wheel'' is a vague expression in this context. Let
me give you an example.

If I want to learn how ``insertion sort'' works, I will read
publications about it and attempt to write it from scratch as if I was
inventing it myself, so I'm re-doing it, copying it, but trying to
make sure that I understand every step of it.

By no means I imply that this is the best method. I do it this way
many times because the knowledge I have tells me so and I'm satisfied
so far.

But you see, this isn't exactly reinventing the wheel because I am
only following someone's steps just to try to see what she saw. And
this is a didactic method I use myself. IMO, this is different from
writing insertion sort in a different way (reinventing the wheel).

[...]
[color=blue]
> In the case of programming, I was being didactic about *technique* issues.
> Not *style* issues. There are many tried and true reasons why globals are
> not usually the best solution for a problem -- and it often does pay to
> learn from people who have tread there in the past.
>
> I was not suggesting a style or preference as better than another. I
> wouldn't do that (well, I try not to), and I think that if you learn
> enough of the "why" behind my blanket "don't do that" statements against
> globals, you would see the difference. Maybe you do.[/color]

What I see is that, specially in PHP, many times global variables are
really not the way to go. I agree with that. I rarely use global
variables in PHP, but I do sometimes, when I need a global
variable. :D Now, you could argue on ``Do you really need it when you
think you need it?'' and we would need specific cases.





Daniel C. Bastos
Guest
 
Posts: n/a
#12: Jul 17 '05

re: how does one access global variables when register_globals is off?


In article <d3miav$c60$1@213-238-64-25.adsl.inetia.pl>, Micha³ Wo¼niak wrote:[color=blue]
> I think we are getting OT here. The question was "should it work" and the
> answer is "yes", unless of course $name is actually a... registered
> global. :)[/color]

Agreed.

[...]
Micha³ Wo¼niak
Guest
 
Posts: n/a
#13: Jul 17 '05

re: how does one access global variables when register_globals is off?


One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable Daniel C. Bastos's handwriting:
[color=blue]
> In article <pan.2005.04.14.19.44.45.538648@example.invalid> , JDS wrote:[color=green]
>> On Thu, 14 Apr 2005 12:03:08 +0000, Daniel C. Bastos wrote:
>>[color=darkred]
>>> I disagree with your didacticism. I tell myself to go ahead and try
>>> my own ideas and see if they work well. If they work well, then they
>>> work well. I consider myself unexperienced in programming in general,
>>> but I think I started progressing when I decided to drop all of these
>>> ``lessons''. I don't think level of knowledge matters, do you?
>>>
>>> I think it's better to think for yourself. Someone tells you ``it's
>>> better to write a function for this one specific case'', you should
>>> think ``Why?'' and see if you can find reasons in favor and against
>>> the statement. I think you truly learn this way.
>>>
>>> [...][/color]
>>
>> Think for yourself, yes. But there is no point in reinventing the
>> wheel.[/color]
>
> I probably agree with you here. I say ``probablly'' because
> reinventing the wheel is a bit necessary sometimes for learning. But
> ``reinventing the wheel'' is a vague expression in this context. Let
> me give you an example.
>
> If I want to learn how ``insertion sort'' works, I will read
> publications about it and attempt to write it from scratch as if I was
> inventing it myself, so I'm re-doing it, copying it, but trying to
> make sure that I understand every step of it.
>
> By no means I imply that this is the best method. I do it this way
> many times because the knowledge I have tells me so and I'm satisfied
> so far.
>
> But you see, this isn't exactly reinventing the wheel because I am
> only following someone's steps just to try to see what she saw. And
> this is a didactic method I use myself. IMO, this is different from
> writing insertion sort in a different way (reinventing the wheel).[/color]

Agreed. I wrote an authentication through SessionID myself in PHP just to
know how it is being done. I could use SESSIONS, but I preferred to
write it myself the first time.
[color=blue]
> [...]
>[color=green]
>> In the case of programming, I was being didactic about *technique*
>> issues.
>> Not *style* issues. There are many tried and true reasons why globals
>> are not usually the best solution for a problem -- and it often does
>> pay to learn from people who have tread there in the past.
>>
>> I was not suggesting a style or preference as better than another. I
>> wouldn't do that (well, I try not to), and I think that if you learn
>> enough of the "why" behind my blanket "don't do that" statements
>> against
>> globals, you would see the difference. Maybe you do.[/color]
>
> What I see is that, specially in PHP, many times global variables are
> really not the way to go. I agree with that. I rarely use global
> variables in PHP, but I do sometimes, when I need a global
> variable. :D Now, you could argue on ``Do you really need it when you
> think you need it?'' and we would need specific cases.[/color]

If I have a value that has to be accessible to all the parts of my script
and has to be possible to modify (which is VERY rare), I will use
globals. Globals are a tool, and like any tool they might be misused.
But, like every tool, they ARE useful sometimes.

Cheers
Mike
Geoff Berrow
Guest
 
Posts: n/a
#14: Jul 17 '05

re: how does one access global variables when register_globals is off?


I noticed that Message-ID:
<pan.2005.04.14.19.44.45.538648@example.invalid> from JDS contained the
following:
[color=blue]
>I was not suggesting a style or preference as better than another. I
>wouldn't do that (well, I try not to), and I think that if you learn
>enough of the "why" behind my blanket "don't do that" statements against
>globals, you would see the difference. Maybe you do.[/color]

I recently wrote a function to add drop shadows to images. The function
uses three image files to create the drop shadows and I define absolute
paths to these as constants.

Is this good or bad use of constsnts/globals?


//original size of shadow images is shown below
define("BOTTOM_SHADOW","/images/bottom_white.jpg");
//387x19
define("RIGHT_SHADOW","/images/right_white.jpg");
//280x19
define("CORNER_SHADOW","/images/corner_white.jpg");
//19x19

function ds($image,$dsw,$alt){
//first check that file exists
if(file_exists($image)){
//create an image from the file
$img=ImageCreateFromJpeg($image);
//print containing <div> using image width plus shadow width
print "<div style='width:
".(imagesx($img)+$dsw)."px;margin:0px'>\n";
//print image
print "<img style='float: left;' src='$image'
height='".imagesy($img)."' width='".imagesx($img)."'alt='$alt'>\n";
//print right shadow using image sizes
print "<img style='float: left;'
src='".RIGHT_SHADOW."'height='".imagesy($img)."'wi dth='$dsw'alt=''>\n";
//print bottom shadow using image sizes
print"<img style='float: left;'
src='".BOTTOM_SHADOW."'width='".imagesx($img)."'he ight='$dsw'alt=''>\n";
//print corner shadow using image sizes
print "<img style='float: left;'
src='".CORNER_SHADOW."'height='$dsw'width='$dsw'al t=''>\n";
//print closing </div>
print "</div>\n";
}
}

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
JDS
Guest
 
Posts: n/a
#15: Jul 17 '05

re: how does one access global variables when register_globals is off?


On Fri, 15 Apr 2005 08:03:03 +0100, Geoff Berrow wrote:
[color=blue]
> Is this good or bad use of constsnts/globals?[/color]

Personally, I think defining constants is the way to go for your example.

--
JDS | jeffrey@go.away.com
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Closed Thread