Very EASY php question

demon42

Member
Jul 19, 2004
160
0
0
What's wrong with this?

Actual Output: "number "
Desired Output: "number two"

What the heck is wrong?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
It works if you write functions that actually take arguments instead of using stupid global variables.

 

demon42

Member
Jul 19, 2004
160
0
0
Thanks, though I'm really trying to get that array to function as a global... what's wrong with making that array global? (except that it isn't working, that is!)

The php script I'm writing would have alot of redundancy if I can't get arrays to work as such. (the function I'm writing uses many global variables)
 
Jun 4, 2005
19,723
1
0
Originally posted by: notfred
Originally posted by: LoKe
I sense agression.

It's towards PHP and its stupid encouragement of bad software design practices.

The only solution is to offer an alternative. I would try, but I would probably just make things worse.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
The alternative is to learn how to use objects. Then you can intialize them and use them. Or pass variables to your functions like a good programer. Global variables are like goto statements. There is never a good reason to use them, except for that one exception, but I"m not going to tell you. Cause if you dont know its a good time to use them, then it's not.
 

cleverhandle

Diamond Member
Dec 17, 2001
3,566
3
81
If you want to use a global variable within a function (which, as notfred correctly points out, is not usually good practice) you need to tell the function that variable is global rather than local. So add a line global $the_array; before you assign the value to $rtrn.

Using lots of globals is discouraged because it quickly leads to code thats difficult to understand and maintain. When your functions are all operating on globals, it's hard to follow the code path because you have to follow each subroutine you call to make sure it's not surreptitiously changing a global variable while you're not looking.