> I've been programming in C all my life, and now I just started developing
> in PHP and I'm really enjoying it, it has all the missing improvements that
> C needed to be more user-likely.
>
> But one thin I can't get, how can PHP call a variabel global, if it isn't
> global.
> A global var, is a var defined outside all functions and it's available
> to all and altered by all, without having to redefine or call the var again.

That may be your definition of a global variable.  It isn't mine.  The
fact that you have some way to access the variable from every scope makes
it global by my definition.

Also, all global variables are available via $GLOBALS['var_name'].  And if
you really do have 50 global variables, you should really appreciate this
feature as your chances of having local/global variable overlaps which can
cause weird bugs is that much higher.  I would suggest grouping your
variables into logical arrays of information and doing a 'global' on these
arrays instead.

> In PHP, for a var to be global you have to add a 'global $var' inside the
> function u want to use it. THis is not nice, what about if u have a form
> with 50 fields and want a function to validate all of them, u have to pass
> them all to the function or build a little piece of code to make all th
> $GLOBALS local right?

If you have a form with 50 fields, name them like this:

<input type="text" name="blah[abc]">
<input type="text" name="blah[def]">
<input type="text" name="blah[ghi]">

Then simply make do: global $blah

> Is this really the idea of global vars?

The idea is to avoid really nasty scope-related bugs that are common in C
code that uses lots of global variables.  Years and years ago when I wrote
the first version of PHP I was working for a telco writing software for a
large telephone switch.  The code was huge and extremely ugly.  Global
variables everywhere.  The team had been chasing a bug for about a week
when I got stubborn and decided it was time to kill the bug.  I printed
out all the source code and laid it out in a long hallway as I crawled
along with different coloured pens and manually traced my way through it
as none of the debuggers we had at the time were of any use.  After
countless hours the bug turned out to be inside a function that silently
modified a global variable which affected another piece of code in a
completely different part of the program.

I swore I would not have the same problem in PHP and thus the requirement
for people to be explicit about using global variables inside functions.
Hopefully it also forces a little bit of structure and organization on
people.

-Rasmus


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to