|
Posted by Steve on 10/13/06 00:30
"windandwaves" <nfrancken@gmail.com> wrote in message
news:1160696898.169641.105680@i3g2000cwc.googlegroups.com...
|
| Steve wrote:
| > "ZeldorBlat" <zeldorblat@gmail.com> wrote in message
| > news:1160656554.301340.255580@m7g2000cwm.googlegroups.com...
| > |
| > | windandwaves wrote:
| > | > Hi Folk
| > | >
| > | > Consider the two code options below. I want to know if it makes
more
| > | > sense, in theory, to pass a variable or to make it global
| > | >
| > | > FIRST OPTION --
| > | >
| > | > ...
| > | >
| > | > $data = 5;
| > | >
| > | > $data = dosomething($data)
| > | >
| > | > ...
| > | >
| > | > echo $data;
| > | > exit();
| > | >
| > | > function dosomething($data) {
| > | > $myval = 3;
| > | > $data = $data + $myval;
| > | > return $data;
| > | > }
| > | >
| > | > SECOND OPTION --
| > | > ...
| > | >
| > | > $data = 5;
| > | >
| > | > dosomething()
| > | >
| > | > ...
| > | >
| > | > echo $data;
| > | > exit();
| > | >
| > | > function dosomething() {
| > | > global $data;
| > | > $myval = 3;
| > | > $data = $data + $myval;
| > | > }
| > |
| > | First option -- always, always, always.
| >
| > unless...$data is a huge representation of information in memory (which
you
| > are short on)
|
| true
|
| > and you're using php < 5
|
| true
|
| >(byval not byref)
|
| Not sure
|
| > and you're calling
| > dosomething() within a loop. ;^)
|
| false....
|
|
| Hmmm, I was close.
|
| My thinking indeed was that if the data was huge and it had to be
| thrown into the function and then back it would make more sense to keep
| it were it was and edit it a little in the function....
as long as it makes sense. you have to remember that php is a scripting
language. it's not formally structured. anyway, i think the overall goal of
code is to be understandable so that it can be maintained. consistency is
key as well as order.
btw, byval means that that huge chunk of data in $data (in example 1) would
be *copied* as it was passed to a function or assigned to another variable.
byref means that only a pointer is passed to the function or variable when
assigned...the place in memory where $data resides. make sense?
Navigation:
[Reply to this message]
|