473,508 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Don’t unset that variable you need globally

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-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825331
Jul 17 '05 #1
21 3354
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.
Jul 17 '05 #2
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.

Berislav
Jul 17 '05 #3
"" wrote:
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-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=825739
Jul 17 '05 #4
"steve" <Us************@dbForumz.com> wrote in message
news:4_***************************************@dbf orumz.com...
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
Jul 17 '05 #5
> 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.

"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
Jul 17 '05 #6
> > 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
Jul 17 '05 #7
"Hilarion" <hi******@SPAM.op.SMIECI.pl> wrote in message
news:da**********@news.onet.pl...
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
Jul 17 '05 #8

"Kimmo Laine" <et******************@Mgmail.com> wrote in message
news:DV*****************@reader1.news.jippii.net.. .
| "Hilarion" <hi******@SPAM.op.SMIECI.pl> wrote in message
| news:da**********@news.onet.pl...
| >> 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. ;^)
Jul 17 '05 #9
> 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:
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.

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"

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"

and it is no longer globally available.
"You do not have acces to the global variable from current (function) scope."

Cost me 10 hours....


Hilarion

PS.: The fact that English is not my native language is probably influencing
the way that I read it.
Jul 17 '05 #10
Berislav Lopac wrote:
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
Jul 17 '05 #11
On 5 Jul 2005 15:38:12 -0400, steve <Us************@dbForumz.com> wrote:
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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #12
In comp.lang.php steve <Us************@dbforumz.com> wrote:
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
Jul 17 '05 #13
> "unset( $_GLOBAL['foo'] )".

Should be:

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

My mistake (I noticed it after reading post of Daniel Tryba).
Hilarion
Jul 17 '05 #14
In comp.lang.php Hilarion <m0rt_nospam_@nospam_bez_spamu_itepe_poczta.onet.p l> 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 :)

Jul 17 '05 #15
"" wrote:
In comp.lang.php Hilarion
<m0rt_nospam_@nospam_bez_spamu_itepe_poczta.onet.p l> 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-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826716
Jul 17 '05 #16
In comp.lang.php steve <Us************@dbforumz.com> 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.

Jul 17 '05 #17
"" wrote:
In comp.lang.php steve <Us************@dbforumz.com> 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-unse...ict237516.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=826852
Jul 17 '05 #18
Tony wrote:
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
Jul 17 '05 #19
| 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.
Jul 17 '05 #20
In comp.lang.php steve <a@b.com> 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";
?>

Jul 17 '05 #21
gotcha.
"Daniel Tryba" <pa**********@invalid.tryba.nl> wrote in message
news:42***********************@news6.xs4all.nl...
| In comp.lang.php steve <a@b.com> 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";
| ?>
|
Jul 17 '05 #22

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

Similar topics

3
14888
by: Phil Powell | last post by:
PHP: unset($_SESSION); In my original environment (PHP 4.3.2) this line will delete the session variable 'mainDisplay'. But in the testing environment (PHP 4.3.6) the variable persists even...
2
3112
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
3
4095
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which...
1
3663
by: Rob Wire | last post by:
Please let me know the preferred way to do the following. Accept a Variable in the calling page's URL of an ID. Set that variable to be global scope of the class so that it can be used throughout...
7
1839
by: fasanay | last post by:
Hi everybody I have got the following PHP code which I am trying to convert to ASP any help will be appreciated...I have done most of it but I cant find a replace function for Unset in asp which...
10
2227
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
2
8944
by: Georg Weiler | last post by:
Hi, I'm biting my nails on this for several days now, hope that someone of you can help me...: On my page, the user can display tables, created out of a database. So I have several <a href>...
7
5223
by: lievendp | last post by:
Just wondering, Is it possible for an instance of a class to unset itself? can I do "unset($this)" or "unset(&$this)" regards, Lieven http://eye.cc php newsgroups
4
45339
by: mtuller | last post by:
I have a page that submits data to a database, and I want to make it so that if the page is refreshed, it doesn't submit the information again. I am trying to use unset the variables so that if the...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.