Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Functions can't access strings defined outside unless they're arguments can they?

0 views
Skip to first unread message

John O. Kopf

unread,
Nov 3, 2009, 7:11:46 PM11/3/09
to
I've found that I can define:

$W = '<img src="'.$Path.'R_Images/Marks/WhiteBall.gif" width="14"
height="14">';

...and can reference this within any PHP code, but I *can't* reference
it from within a PHP function, unless it's been passed as an argument.

I can't find this documented anywhere, but it appears to fit all of the
observations.

If *not* true, can someone tell me how to reference such a string from
within a function *without* passing it as an argument?

Thanks.

John Kopf

rf

unread,
Nov 3, 2009, 7:26:19 PM11/3/09
to

"John O. Kopf" <ko...@att.net> wrote in message
news:hcqgr...@news3.newsguy.com...

> I've found that I can define:
>
> $W = '<img src="'.$Path.'R_Images/Marks/WhiteBall.gif" width="14"
> height="14">';
>
> ...and can reference this within any PHP code, but I *can't* reference it
> from within a PHP function, unless it's been passed as an argument.

Unless you use the global keyword within your function, as in
global $w;

Jerry Stuckle

unread,
Nov 3, 2009, 8:05:21 PM11/3/09
to

That is correct, and it is intentional. See
http://www.php.net/manual/en/language.variables.scope.php.

You can use the global keyword - but much better is to pass the variable
as an argument to the function.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

The Natural Philosopher

unread,
Nov 4, 2009, 4:20:02 AM11/4/09
to

look up the global command
> Thanks.
>
> John Kopf
>

John O. Kopf

unread,
Nov 4, 2009, 7:28:32 PM11/4/09
to

"Look up" Where? I used it, and it solved my problems, but where is it
described? Google "global command" or "global" and you get a million
hits, none of which appear to apply, before giving up :>{

John Kopf

Jerry Stuckle

unread,
Nov 4, 2009, 8:12:21 PM11/4/09
to

You do NOT want to use global, for many reasons. You should pass the
item as a function parameter.

Don't worry about what TNP says - his advice is seldom accurate, and
even more rarely is it good.

Malcolm Dew-Jones

unread,
Nov 4, 2009, 7:13:47 PM11/4/09
to
John O. Kopf (ko...@att.net) wrote:

well since this is php, how about
google: php global
for me the very first result is the php documentation for it.

richard

unread,
Nov 4, 2009, 11:45:17 PM11/4/09
to

http://php.net/manual/en/language.variables.scope.php

Try reading this page for some clues.

The Natural Philosopher

unread,
Nov 5, 2009, 5:26:13 AM11/5/09
to

In this case it is accurate, and its up to the OP to decide whether its
'good' or not, not you.

Jerry Stuckle

unread,
Nov 5, 2009, 5:54:45 AM11/5/09
to

Nope, use of globals is bad - in any DECENT programmer's book. For
reasons beyond your understanding, because it requires intelligence.

Now go back to your ditch digging. Here's a tip - the wide end of the
shovel goes in the ground.

rf

unread,
Nov 5, 2009, 6:40:45 AM11/5/09
to

"Jerry Stuckle" <jstu...@attglobal.net> wrote in message
news:hcuatm$h84$2...@news.eternal-september.org...

IBTD.

<snip strawman argument>

So why, for instance, is $_POST automatically global?

And why, for instance is $_SESSION automatically global?

And why, for instance, should I not make my used-every-bloody-where variable
$database global. Do you expect me to pass the name of my database as a
variable to each and every single function that just might make a call on
that database?

And every DECENT programming language has provision for making certain
variables global. Especially the one I use day to day, C++, although C#
seems to have fucked it up.

Jerry, just because *you* do it that way does not mean that *all* of us must
do it that way as well. If you feel so strongly about the global keyword
then please cite your objection to the people who wrote PHP.

<snip strawman argument>


Jerry Stuckle

unread,
Nov 5, 2009, 7:11:24 AM11/5/09
to

These have nothing to do with the use of the global keyword. YOU are
making strawman arguments.

> And why, for instance, should I not make my used-every-bloody-where variable
> $database global. Do you expect me to pass the name of my database as a
> variable to each and every single function that just might make a call on
> that database?
>

Yep. The function is now independent of the name of the variable in
rest of the code. You can call the function from different areas of the
code, using different variable names.

For instance - what if you need to open TWO different connections to a
database? Your functions now fail on one of those connections, because
it is more closely tied to the rest of the program than necessary. It
also increases maintenance costs and more potential problems when
modifying the code.

Well written functions are independent of anything in the code other
than what is passed to them. This is true in ANY language supporting
functions.

> And every DECENT programming language has provision for making certain
> variables global. Especially the one I use day to day, C++, although C#
> seems to have fucked it up.
>

Another strawman argument. That does not mean they are good. C has a
"goto" statement, also. But even Kernighan and Ritchie admitted it was
a bad thing.

And if globals are so great, why even have variable scope? Just make
everything global (like COBOL did).

> Jerry, just because *you* do it that way does not mean that *all* of us must
> do it that way as well. If you feel so strongly about the global keyword
> then please cite your objection to the people who wrote PHP.
>
> <snip strawman argument>
>
>

Yet a third strawman argument. This one isn't even worth responding to.

I can't help it if you write terrible code like TNP. But please don't
encourage others to follow your examples.

rf

unread,
Nov 5, 2009, 7:25:28 AM11/5/09
to

"Jerry Stuckle" <jstu...@attglobal.net> wrote in message
news:hcufdg$kr5$1...@news.eternal-september.org...

rf

unread,
Nov 5, 2009, 7:26:59 AM11/5/09
to

"Jerry Stuckle" <jstu...@attglobal.net> wrote in message
news:hcufdg$kr5$1...@news.eternal-september.org...


Stuckly, you are really and truly a fuckwit.


bill

unread,
Nov 5, 2009, 9:46:55 AM11/5/09
to

After considerable parsing, it appears that this comment does not
add to the discussion. Go to your room.

bill

Jerry Stuckle

unread,
Nov 5, 2009, 10:15:05 AM11/5/09
to

Gee, you can't continue a logical discussion so you have to come back
with personal insults.

How like the troll.

You should really read some books on program design. You might learn
something.

The Natural Philosopher

unread,
Nov 5, 2009, 11:43:33 AM11/5/09
to

Oh dear ooh dear. Why dio the Gods of PHP include it then?

Never mind jerry, its a grown up tool for grown ups and dangerous in
childish hands, so you stick to your gbuns OK and eat your greems and
you might grow up to be a Real Progranmmer yet.

For
> reasons beyond your understanding, because it requires intelligence.
>
> Now go back to your ditch digging. Here's a tip - the wide end of the
> shovel goes in the ground.

Oh dear. You don't dig with shovels, and the narrow end of a spade
called the blade, goes in the ground. The wider put is where your hand go.

Therese a scientific explanation as to why blades are thin and handles
thick, but you wouldn't understand it. It has numbers and formulae in it.

Sigh. Such a waste. Not even fit to dig a ditch.

>

The Natural Philosopher

unread,
Nov 5, 2009, 11:44:33 AM11/5/09
to

The Natural Philosopher

unread,
Nov 5, 2009, 11:48:53 AM11/5/09
to
What too you so long?

Malcolm Dew-Jones

unread,
Nov 5, 2009, 11:42:05 AM11/5/09
to
John O. Kopf (ko...@att.net) wrote:

Since this is about php, I would try

google: php global

works for me.

John O. Kopf

unread,
Nov 5, 2009, 12:45:09 PM11/5/09
to
Malcolm Dew-Jones wrote:
> John O. Kopf (ko...@att.net) wrote:
> : The Natural Philosopher wrote:
> : > John O. Kopf wrote:
> : >> I've found that I can define:
> : >>
SNIP

> : >
> : > look up the global command
> : >> Thanks.
> : >>
> : >> John Kopf
> : >>
>
> : "Look up" Where? I used it, and it solved my problems, but where is it
> : described? Google "global command" or "global" and you get a million
> : hits, none of which appear to apply, before giving up :>{
>
> well since this is php, how about
> google: php global
> for me the very first result is the php documentation for it.

Sorry, I wasn't clear...

Nowhere in the PHP pages I've found, using Google, was there any
reference to "global" - thus, not knowing the word to use, I couldn't
google it.

What I need is a (hopefully, pdf) manual or specification for PHP that I
can download and refer to off-line, and which is complete enough to
actually mention things like "global"...Most just give lists of the
common string functions, with (VERY) simple examples.

John Kopf

The Natural Philosopher

unread,
Nov 5, 2009, 12:55:05 PM11/5/09
to
You can define a variable outside of any function and access it from
within a function by declaring it *within* the function with the global
keyword.


Beware however, that any other function in any (included) program file
can so access it.

Which may be a blessing, or a curse, depending..

Peter H. Coffin

unread,
Nov 5, 2009, 1:42:23 PM11/5/09
to
On Thu, 05 Nov 2009 16:43:33 +0000, The Natural Philosopher wrote:
> Jerry Stuckle wrote:
>> The Natural Philosopher wrote:
>>> In this case it is accurate, and its up to the OP to decide whether
>>> its 'good' or not, not you.
>>>
>>
>> Nope, use of globals is bad - in any DECENT programmer's book.
>
> Oh dear ooh dear. Why dio the Gods of PHP include it then?

Sometimes people don't care to do things properly. "Half-assed in half
the time" and all that. That doesn't mean it's a good idea to advise it
or to label it as "good". It's still bad, just expedient.

> Never mind jerry, its a grown up tool for grown ups and dangerous in
> childish hands, so you stick to your gbuns OK and eat your greems and
> you might grow up to be a Real Progranmmer yet.

I hate cleaning up after "Real Programmers". Gimme a well-trained n00b
any day...

> For
>> reasons beyond your understanding, because it requires intelligence.
>>
>> Now go back to your ditch digging. Here's a tip - the wide end of the
>> shovel goes in the ground.
>
> Oh dear. You don't dig with shovels, and the narrow end of a spade
> called the blade, goes in the ground. The wider put is where your hand go.
>
> Therese a scientific explanation as to why blades are thin and handles
> thick, but you wouldn't understand it. It has numbers and formulae in it.
>
> Sigh. Such a waste. Not even fit to dig a ditch.

I admire your use of pedantic irrelevancies, but you can't distract me
or anyone else with them.

--
"This place is evil! We need weapons! Crossbows! Rocket Launchers!
Rent-a-zilla!"
-- L33t Master Largo www.megatokyo.com

The Natural Philosopher

unread,
Nov 5, 2009, 2:24:22 PM11/5/09
to
Peter H. Coffin wrote:
> On Thu, 05 Nov 2009 16:43:33 +0000, The Natural Philosopher wrote:
>> Jerry Stuckle wrote:
>>> The Natural Philosopher wrote:
>>>> In this case it is accurate, and its up to the OP to decide whether
>>>> its 'good' or not, not you.
>>>>
>>> Nope, use of globals is bad - in any DECENT programmer's book.
>> Oh dear ooh dear. Why dio the Gods of PHP include it then?
>
> Sometimes people don't care to do things properly. "Half-assed in half
> the time" and all that. That doesn't mean it's a good idea to advise it
> or to label it as "good". It's still bad, just expedient.
>
>> Never mind jerry, its a grown up tool for grown ups and dangerous in
>> childish hands, so you stick to your gbuns OK and eat your greems and
>> you might grow up to be a Real Progranmmer yet.
>
> I hate cleaning up after "Real Programmers". Gimme a well-trained n00b
> any day...
>

Then they aren't real programers are they?


>> For
>>> reasons beyond your understanding, because it requires intelligence.
>>>
>>> Now go back to your ditch digging. Here's a tip - the wide end of the
>>> shovel goes in the ground.
>> Oh dear. You don't dig with shovels, and the narrow end of a spade
>> called the blade, goes in the ground. The wider put is where your hand go.
>>
>> Therese a scientific explanation as to why blades are thin and handles
>> thick, but you wouldn't understand it. It has numbers and formulae in it.
>>
>> Sigh. Such a waste. Not even fit to dig a ditch.
>
> I admire your use of pedantic irrelevancies, but you can't distract me
> or anyone else with them.
>

Global's have their place: That's why they are included.

If you have one and one only master data structure in a program its a
waste of time to keep copying it around and passing it mindlessly down
through one function after another, especially when you can't even get
it permanently altered by any of them unless you return a value all the
way up the nest to set something in it, and then what happens to your
error returns? sure you can pass by reference, but that IS what the
global keyword does *anyway*.

No one is advocating removing local variables from the language: merely
recognising the usefulness of global scopes, and its pitfalls.

PHP has its own global variables built in, for good reasons. You can
extend them as you see fit.


Good program design is not slavishly following rules. Its about
understanding when its actually harder and trickier and more lexically
confusing to to keep to them, than to break them.

Malcolm Dew-Jones

unread,
Nov 5, 2009, 1:59:35 PM11/5/09
to
Malcolm Dew-Jones (yf...@vtn1.victoria.tc.ca) wrote:

: google: php global

: works for me.

Why are my posts turning up in a different thread? I didn't see my
earlier response and thought I had imagined sending it, now I see it here.
Oh well, oddities abound.

Peter H. Coffin

unread,
Nov 5, 2009, 3:04:32 PM11/5/09
to
On Thu, 05 Nov 2009 19:24:22 +0000, The Natural Philosopher wrote:
> No one is advocating removing local variables from the language: merely
> recognising the usefulness of global scopes, and its pitfalls.
>
> PHP has its own global variables built in, for good reasons. You can
> extend them as you see fit.
>
> Good program design is not slavishly following rules. Its about
> understanding when its actually harder and trickier and more lexically
> confusing to to keep to them, than to break them.

"The Problem" is that, breaking "the rules" not only requires good
understanding by the particular programmer writing the code, but
everyone thereafter that's supposed to modify it has to understand the
rules in exactly the same way and get into the head of that particular
programmer in the same kind of way in order to work with the program
code effectively. That's HARD to do, and were more programmers willing
to be less "Real Programmer" hardcore and code very conservatively and
very close to some commonly-accepted best practices, it makes them more
like each other and less of a pain in the ass to get into their heads.

For example, a coworker of mine has a stylistic thing where he loathes
testing complicated conditions. So, before anything that's really
complicated he'll start setting flag variables and things far up in the
code before the condition test, sometimes multiple times changing the
value, or assigning more than one flag at a check, which means every
damn time I want to follow the value of a variable, I end up having to
follow five or six instead, just to see the consequences.

--
1. My Legions of Terror will have helmets with clear plexiglass visors,
not face-concealing ones.
--Peter Anspach's list of things to do as an Evil Overlord

The Natural Philosopher

unread,
Nov 5, 2009, 4:43:50 PM11/5/09
to
Peter H. Coffin wrote:
> On Thu, 05 Nov 2009 19:24:22 +0000, The Natural Philosopher wrote:
>> No one is advocating removing local variables from the language: merely
>> recognising the usefulness of global scopes, and its pitfalls.
>>
>> PHP has its own global variables built in, for good reasons. You can
>> extend them as you see fit.
>>
>> Good program design is not slavishly following rules. Its about
>> understanding when its actually harder and trickier and more lexically
>> confusing to to keep to them, than to break them.
>
> "The Problem" is that, breaking "the rules" not only requires good
> understanding by the particular programmer writing the code, but
> everyone thereafter that's supposed to modify it has to understand the
> rules in exactly the same way and get into the head of that particular
> programmer in the same kind of way in order to work with the program
> code effectively. That's HARD to do, and were more programmers willing
> to be less "Real Programmer" hardcore and code very conservatively and
> very close to some commonly-accepted best practices, it makes them more
> like each other and less of a pain in the ass to get into their heads.
>

Oh, indeed, but the presence of the Global keyword is a bit of a giveaway..

Unlike C, a global variable is explicitly defined within a function. So
you are always aware at the function level you are dealing with it.

A quick 'grep global *.php' will show you who is fiddling with it.

> For example, a coworker of mine has a stylistic thing where he loathes
> testing complicated conditions. So, before anything that's really
> complicated he'll start setting flag variables and things far up in the
> code before the condition test, sometimes multiple times changing the
> value, or assigning more than one flag at a check, which means every
> damn time I want to follow the value of a variable, I end up having to
> follow five or six instead, just to see the consequences.
>

Kick him in the balls.

If I do a line of complicated conditionals, there is usually three to 5
lines of comment above it. Heck even though I doubt anyone else will
ever fix bugs in it, it helps ME 6 months down the line when I have to
fix some obscure condition, to see what it was SUPPOSED to do. Even if
the code fails to achieve its aims..;-)

I learnt that coding assembler..mostly assembler bears no lexical
correlation to the functionality of the code. It NEEDS comments, lots of
em. Not one per line as the 'rules' said, but certainly a big one per
logical block.

Anyway global variables are dead useful in e.g. state machines, and in
error handling. There is a tendency to obscure them in OOP buy binding
them a static data inside 0bjects and using methods to get at them, but
the result is really the same. Its just cleaner and less overhead to use
a global..


Jerry Stuckle

unread,
Nov 5, 2009, 10:45:29 PM11/5/09
to

I think your meaning went completely over TNP's head.

Of course, that's not surprising. But hopefully others understand.

Danny Wilkerson

unread,
Nov 6, 2009, 6:13:27 AM11/6/09
to
I was using php for months before i found out about globals. LOL i
thought to myself, hey javascript isn't like this. It's funny now but
at the time i was throwing my keyboard at stuff out of frustration.

John O. Kopf

unread,
Dec 31, 2009, 7:19:22 PM12/31/09
to
Peter H. Coffin wrote:
> SNIP

>
> For example, a coworker of mine has a stylistic thing where he loathes
> testing complicated conditions. So, before anything that's really
> complicated he'll start setting flag variables and things far up in the
> code before the condition test, sometimes multiple times changing the
> value, or assigning more than one flag at a check, which means every
> damn time I want to follow the value of a variable, I end up having to
> follow five or six instead, just to see the consequences.
>

I once knew a FORTRAN! :>} programmer (some 40 years ago!) who was so
proud that all his labels were BINARY (010001, 010010. etc.),and all of
his fixed point variables started with "I" and floating variables with
"O", and the rest of each variable name was a mix of 0, 1, O, and I!

Impossible to read his code!!!!

John Kopf

crim...@googlemail.com

unread,
Jan 2, 2010, 1:33:02 PM1/2/10
to
On 5 ноя 2009, 19:45, "John O. Kopf" <ko...@att.net> wrote:

> What I need is a (hopefully, pdf) manual or specification for PHP that I
> can download and refer to off-line, and which is complete enough to
> actually mention things like "global"...Most just give lists of the
> common string functions, with (VERY) simple examples.
>

You can download docs here: http://www.php.net/download-docs.php
But better to look online because you can find very usefull comments
on site.
To find page about something just add this to PHP site name. For
example http://www.php.net/global to see variable score page with
description about 'global' keyword.

crim...@googlemail.com

unread,
Jan 2, 2010, 1:44:07 PM1/2/10
to
On 5 ноя 2009, 13:40, "rf" <r...@z.invalid> wrote:
> So why, for instance, is $_POST automatically global?
> And why, for instance is $_SESSION automatically global?
I can add magic_quotes_*, includes default folder, MySQL connection
caching and other fully idiotic things to this list. Sorry to say but
authors of PHP are not authorities in computer science.
0 new messages