Help - Search - Member List - Calendar
Full Version: Dont unset that variable you need globally
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
steve
Dont make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO: $var = null;
DONT DO: unset($var);

Why? Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unset-vari...pict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825331

Exyle
steve wrote:
QUOTE
Dont make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

Thanks for the heads up, although I do remember reading something along

these lines in the PHP manual.

Berislav Lopac
steve wrote:
QUOTE
Dont make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

Even better -- don't use global variables.

Berislav

steve
"" wrote:
QUOTE
steve wrote:
Dont make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name
space,
and it is no longer globally available. Cost me 10 hours....

Thanks for the heads up, although I do remember reading
something along
these lines in the PHP manual.

You are welcome, and if you find that inf on php site, please post.

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unset-vari...pict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825739

Kimmo Laine
"steve" <[Email Removed]> wrote in message
news:[Email Removed]...
QUOTE
Dont make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

That's odd. The manual seems to disagree:

"If a globalized variable is unset() inside of a function, only the local
variable is destroyed. The variable in the calling environment will retain
the same value as before unset() was called.

<?php
function destroy_foo()
{
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo; // prints "bar"
?>"
- from http://fi.php.net/manual/en/function.unset.php

Now I don't know what to believe..

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com

Hilarion
QUOTE
Steve wrote:
Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

Kimmo Laine wrote:
QUOTE
That's odd. The manual seems to disagree:

"If a globalized variable is unset() inside of a function, only the local
variable is destroyed. The variable in the calling environment will retain
the same value as before unset() was called.

<?php
function destroy_foo()
{
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo; // prints "bar"
?>"
- from http://fi.php.net/manual/en/function.unset.php

That's exactly what Steve said. He said that the variable is removed
from current namespace (function namespace), not that it's totaly
removed.

"global" keyword is like setting local reference to global variable.
When you call "unset", then you unset reference to that variable.
To "unset" global variable from a function you'll have to
"unset( $_GLOBAL['foo'] )".


Hilarion

Hilarion
QUOTE
steve wrote:
Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name
space,
and it is no longer globally available. Cost me 10 hours....

[...] if you find that inf on php site, please post.

Read Kimmo Laine post. It contains the link and manual fragment
which describes the case ("the local variable is destroyed").

Hilarion

Kimmo Laine
"Hilarion" <[Email Removed]> wrote in message
news:dadovd$8n8$[Email Removed]...
QUOTE
Steve wrote:
Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

Kimmo Laine wrote:
That's odd. The manual seems to disagree:

"If a globalized variable is unset() inside of a function, only the local
variable is destroyed. The variable in the calling environment will
retain the same value as before unset() was called.

<?php
function destroy_foo()
{
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo; // prints "bar"
?>"
- from http://fi.php.net/manual/en/function.unset.php

That's exactly what Steve said. He said that the variable is removed
from current namespace (function namespace), not that it's totaly
removed.


I somehow thought he meant from the entire namespace, not from current...
Okay, my bad. Now it all makes sense.

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com

steve
"Kimmo Laine" <[Email Removed]> wrote in message
news:DVtye.2045$[Email Removed]...
| "Hilarion" <[Email Removed]> wrote in message
| news:dadovd$8n8$[Email Removed]...
| >> Steve wrote:
| >> > Say you have defined a variable $var in your main script.
| >> >
| >> > Now in a function you access it using:
| >> > global $var;
| >> >
| >> > But you want to set it to null inside the function.
| >> > DO: $var = null;
| >> > DONT DO: unset($var);
| >> >
| >> > Why? Because the 2nd way removes the variable from the name space,
| >> > and it is no longer globally available. Cost me 10 hours....
| >
| > Kimmo Laine wrote:
| >> That's odd. The manual seems to disagree:
| >>
| >> "If a globalized variable is unset() inside of a function, only the
local
| >> variable is destroyed. The variable in the calling environment will
| >> retain the same value as before unset() was called.
| >>
| >> <?php
| >> function destroy_foo()
| >> {
| >> global $foo;
| >> unset($foo);
| >> }
| >>
| >> $foo = 'bar';
| >> destroy_foo();
| >> echo $foo; // prints "bar"
| >> ?>"
| >> - from http://fi.php.net/manual/en/function.unset.php
| >
| > That's exactly what Steve said. He said that the variable is removed
| > from current namespace (function namespace), not that it's totaly
| > removed.
|
|
| I somehow thought he meant from the entire namespace, not from current...
| Okay, my bad. Now it all makes sense.

hmmm...i don't believe you have mis-read anything. i read steve's op that
same way you did *and* the problem he describes *does* conflict with the php
manual. but then again, we may both be having problems with our reading
comprehension. ;^)

Hilarion
QUOTE
hmmm...i don't believe you have mis-read anything. i read steve's op that
same way you did *and* the problem he describes *does* conflict with the php
manual. but then again, we may both be having problems with our reading
comprehension. ;^)


:)

OK. The way I read it was:

QUOTE
Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

I suppose the above text was clear and requires no translation.


QUOTE
But you want to set it to null inside the function.

The above means "you want to unset global variable $var / set it
to null"


QUOTE
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name space,

"Because the 2nd way removes the variable from current (function) namespace"


QUOTE
and it is no longer globally available.

"You do not have acces to the global variable from current (function) scope."


QUOTE
Cost me 10 hours....



Hilarion

PS.: The fact that English is not my native language is probably influencing
the way that I read it.

Tony
Berislav Lopac wrote:
QUOTE
steve wrote:
Dont' make my mistake. It is costly.

Say you have defined a variable $var in your main script.

Now in a function you access it using:
global $var;

But you want to set it to null inside the function.
DO:  $var = null;
DONT DO:  unset($var);

Why?  Because the 2nd way removes the variable from the name space,
and it is no longer globally available. Cost me 10 hours....

Even better -- don't use global variables.

Easier said than done. There are times when globals are the best solution.
There IS a reason they're available...

--
Tony Garcia
Web Right! Development

Andy Hassall
On 5 Jul 2005 15:38:12 -0400, steve <[Email Removed]> wrote:

QUOTE
That was my point, sorry for causing confusion.  Thanks for reference.


So unset does not work inside a function for global vars- it only
destroys local var.

The workaround is to say
$var = null;
inside functions.

Although the variable remains set that way - it's just null.

To get rid of it completely: unset($GLOBALS['var']);

--
Andy Hassall / <[Email Removed]> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool

Daniel Tryba
In comp.lang.php steve <[Email Removed]> wrote:
QUOTE
This is to my counter-intuitive, and unless you read the manual, one
would not know.

$DEITY forbid that anyone should read the manual!

But one could consider the global keyword to be extremely evil (changing
scopes of variables is extremly yuck), use the superglobal $GLOBALS
instead (note the unintuitive missing '_').

FUP to comp.lang.php

Hilarion
QUOTE
"unset( $_GLOBAL['foo'] )".

Should be:

"unset( $GLOBALS['foo'] )"

My mistake (I noticed it after reading post of Daniel Tryba).


Hilarion

Daniel Tryba
In comp.lang.php Hilarion <m0rt_nospam_@nospam_bez_spamu_itepe_poczta.onet.pl> wrote:
QUOTE
"unset( $_GLOBAL['foo'] )".
Should be:

"unset( $GLOBALS['foo'] )"

My mistake (I noticed it after reading post of Daniel Tryba).

Hehehe :)

A good thing I missed your posting and essentially wrote the same as in
a double blindfolded second opinion test :)

steve
"" wrote:
QUOTE
In comp.lang.php Hilarion
<m0rt_nospam_@nospam_bez_spamu_itepe_poczta.onet.pl> wrote:
"unset( $_GLOBAL['foo'] )".
Should be:

"unset( $GLOBALS['foo'] )"

My mistake (I noticed it after reading post of Daniel
Tryba).

Hehehe :)

A good thing I missed your posting and essentially wrote the
same as in
a double blindfolded second opinion test :)

ok, so what is the difference between:

unset($var)
and
$var = null;

I know the first form removes the $var from name space, but is there
any functions to check the difference. isset() responds the same way
to both.

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unset-vari...pict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826716

Daniel Tryba
In comp.lang.php steve <[Email Removed]> wrote:
QUOTE
ok, so what is the difference between:

unset($var)
and
$var = null;

I know the first form removes the $var from name space, but is there
any functions to check the difference.  isset() responds the same way
to both.

http://php.net/isset says it all:

"isset() will return FALSE if testing a variable that has been set to
NULL."

I have no idea why (and who decided that) a variable set to null is
considered unset (and a reference to a variable with value null not
raising a "Undefined variable" notice). IMHO it simply should return
true since it is just plain inconsistent.

steve
"" wrote:
QUOTE
In comp.lang.php steve <[Email Removed]> wrote:
ok, so what is the difference between:

unset($var)
and
$var = null;

I know the first form removes the $var from name space, but
is there
any functions to check the difference.  isset() responds the
same way
to both.

http://php.net/isset says it all:

"isset() will return FALSE if testing a variable that has been
set to
NULL."

I have no idea why (and who decided that) a variable set to
null is
considered unset (and a reference to a variable with value
null not
raising a "Undefined variable" notice). IMHO it simply should
return
true since it is just plain inconsistent.

I am looking for something like defined(CONSTANT) but I guess not..

--
Posted using the http://www.dbforumz.com interface, at author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbforumz.com/PHP-Don-unset-vari...pict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826852

Berislav Lopac
Tony wrote:
QUOTE
Berislav Lopac wrote:
Even better -- don't use global variables.

Easier said than done. There are times when globals are the best
solution. There IS a reason they're available...

You can (and should) always pass a global variable as a function parameter.
There is no effective difference between:

function foo($bar)
{
global $foobar;
return $bar + $foobar;
}

and

function foo($bar, &$foobar)
{
return $bar + $foobar;
}

The difference is that when you call the second example you have to pass one
more argument, but that prevents the encapsulation of the function, making
it reusable and independent of the outside world.

Berislav

steve
| I am looking for something like defined(CONSTANT) but I guess not..

what?

you're crossing my wires. are you trying to define a constant or work with
variable scope? why not just post your code that's giving you fits. i'd be
willing to bet that the problem is not with either constants or globals.
but, that's just me.

Daniel Tryba
In comp.lang.php steve <[Email Removed]> wrote:
QUOTE
| I am looking for something like defined(CONSTANT) but I guess not..

what?

you're crossing my wires. are you trying to define a constant or work with
variable scope? why not just post your code that's giving you fits. i'd be
willing to bet that the problem is not with either constants or globals.
but, that's just me.

No no, Steve was asking an existentialistic question:

How to check if a variable is set?

isset() is not the answer to the question, easily demonstrated with a
little script which outputs:
$ php4 ./huh.php
isset(foo): false, isset(bar): false

Notice: Undefined variable: bar in /tmp/huh.php on line 8
foo: [], bar: []

Both $foo and $bar aren't set (according ot isset()), using them anyway
results in an undefined notice for $bar only. So $foo is defined. But
defined() is for contants only.

So the question remains how to findout if a variable is set. A
workaround for variables in the global scope exists by checking for the
key in the $GLOBALS array:
keyexists(foo): true, keyexists(bar): false

But how can this check be computed for variables in any other scope?

My sample code:
<?php
error_reporting(E_ALL);

$foo=null;

echo "isset(foo): ".(isset($foo)?"true":"false").", isset(bar): ".(isset($bar)?"true":"false")."n";

echo "foo: [$foo], bar: [$bar]n";

echo "keyexists(foo): ".(array_key_exists('foo',$GLOBALS)?"true":"false").", keyexists(bar): ".(array_key_exists('bar',$GLOBALS)?"true":"false")."n";
?>

steve
gotcha.


"Daniel Tryba" <[Email Removed]> wrote in message
news:42cbf146$0$87250$[Email Removed]...
| In comp.lang.php steve <[Email Removed]> wrote:
| > | I am looking for something like defined(CONSTANT) but I guess not..
| >
| > what?
| >
| > you're crossing my wires. are you trying to define a constant or work
with
| > variable scope? why not just post your code that's giving you fits. i'd
be
| > willing to bet that the problem is not with either constants or globals.
| > but, that's just me.
|
| No no, Steve was asking an existentialistic question:
|
| How to check if a variable is set?
|
| isset() is not the answer to the question, easily demonstrated with a
| little script which outputs:
| $ php4 ./huh.php
| isset(foo): false, isset(bar): false
|
| Notice: Undefined variable: bar in /tmp/huh.php on line 8
| foo: [], bar: []
|
| Both $foo and $bar aren't set (according ot isset()), using them anyway
| results in an undefined notice for $bar only. So $foo is defined. But
| defined() is for contants only.
|
| So the question remains how to findout if a variable is set. A
| workaround for variables in the global scope exists by checking for the
| key in the $GLOBALS array:
| keyexists(foo): true, keyexists(bar): false
|
| But how can this check be computed for variables in any other scope?
|
| My sample code:
| <?php
| error_reporting(E_ALL);
|
| $foo=null;
|
| echo "isset(foo): ".(isset($foo)?"true":"false").", isset(bar):
".(isset($bar)?"true":"false")."n";
|
| echo "foo: [$foo], bar: [$bar]n";
|
| echo "keyexists(foo):
".(array_key_exists('foo',$GLOBALS)?"true":"false").", keyexists(bar):
".(array_key_exists('bar',$GLOBALS)?"true":"false")."n";
| ?>
|


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.