473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

global or $GLOBALS

Hi!
Just two short questions..:

1) In a function I want to get the value of a global variable declared
outside the function. Should I use "global $variable" or $GLOBALS[variable]?
Are there any differences in performance or other aspects which should make
me choose the one thing or the other?

2) Generally, is it better to pass arguments to the function instead?

Ole J
May 18 '06 #1
3 1971
ojorus wrote:
Hi!
Just two short questions..:
Hi,

1) In a function I want to get the value of a global variable declared
outside the function. Should I use "global $variable" or
$GLOBALS[variable]? Are there any differences in performance or other
aspects which should make me choose the one thing or the other?
Just use:
global $varname;
inside your function.

Don't worry about performance issues for such basic stuff unless you have a
valid reason.

2) Generally, is it better to pass arguments to the function instead?
That depends.
I think you mean if passing by reference or plain passing?

Passing by reference is faster, because normal passing involves a complete
copy of the variable.
If you pass a variable by reference, the pointer to the object is passed.
a possible handy or harmfull sideeffect of passing by reference is that the
function can change the content of the passed variable.

Read more here:
http://nl3.php.net/manual/en/language.references.php

Happy coding. :-)

regards,
Erwin Moller

Ole J


May 18 '06 #2
ojorus wrote:
Hi!
Just two short questions..:

1) In a function I want to get the value of a global variable declared
outside the function. Should I use "global $variable" or $GLOBALS[variable]?
Are there any differences in performance or other aspects which should make
me choose the one thing or the other?
Access through $GLOBALS is slightly slower, since it requires two
look-ups: one for the $GLOBALS array, another for the actual element
within the array. Using the global keyword is the preferred method.
2) Generally, is it better to pass arguments to the function instead?


Yes, it's in general better to pass arguments. It makes your code more
flexible obviously as you can pass in different arguments. Using global
variables also makes your functions non-reentrant, which is serious
headache in any complex application.

May 18 '06 #3
Also sprach Erwin Moller:
Passing by reference is faster, because normal passing involves a
complete copy of the variable.


No. See http://www.zend.com/zend/art/ref-count.php

Quote: "Note that PHP 4 is not like C: passing variables by reference is not
necessarily faster than passing them by value. Indeed, in PHP 4 it is
usually better to pass a variable by value, except if the function changes
the passed value or if a reference ... is being passed."

PHP simply creates another entry in the symbol table pointing to the same
stored value and does not copy it until one of the variables is actually
changed. Thus, references would not necessarily be faster.

Greetings,
Thomas
May 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
0
by: miguel | last post by:
I have a number of global variables declared, like so: /** * @global string $GLOBALS * @name $foo */ $GLOBALS = 'something'; /** * @global string $GLOBALS
8
by: Jan-Henrik Grobe | last post by:
Hallo, normally I am not the one who calls a Newsgroup if something does not work. But now I am frustrated. I did write a program with a lot classes. I use some gloabl variables which are in a...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
8
by: Fernan Bolando | last post by:
I have been going through some of the tutorials on the internet regarding coding styles in unix C programming. Most of the stuff I have read do not prefer using global variables, But for multi-file...
10
by: ankisharma | last post by:
Hi all At many places I have seen that programmers pass global variables to functions in c. I am not able to figure out why they do so. need some clues on this. somewhere i heard that this...
10
by: m.epper | last post by:
Hi to everybody. First of all sorry for my english, I'm italian. How can I execute a portion of code, in a function, into the global scope? Example: <?php
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
12
by: a | last post by:
def fn(): for i in range(l) global count count= .... how do i declare count to be global if it is an array subsequently i should access or define count as an array error:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.